【问题标题】:JSF CSV download in bean在 bean 中下载 JSF CSV
【发布时间】:2013-02-04 18:57:12
【问题描述】:

我正在尝试以与此处相同的方式下载 csv: How to provide a file download from a JSF backing bean?

我的回复不断在output.write() 行抛出nullPointerException。 bean 属于请求范围。关于空指针的任何想法?

    try
    {
        //submitForm();
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();

        response.reset();
        response.setContentType("text/csv"); 
        //response.setContentLength(contentLength); 
            response.setHeader ( "Content-disposition", "attachment; filename=\"Reporting-" + 
                    new Date().getTime() + ".csv\"" );

        OutputStream output = response.getOutputStream();
        String s = "\"Project #\",\"Project Name\",\"Product Feature(s)\",";
        s+="\"Project Status\",";
        s+="\"Install Type\",";
        s+="\"Beta Test\",\"Beta Test New/Updated\",";
        s+="\"Production\",\"Production New/Updated\",";
        s+="\n";
        InputStream is = new ByteArrayInputStream( s.getBytes("UTF-8") );
        int nextChar;

         while ((nextChar = is.read()) != -1) 
         {
            output.write(nextChar);
         }
         output.close();

    }
    catch ( IOException e )
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

【问题讨论】:

  • 这不是 JSF 直接问题。你需要convert String into a Stream。另外,不要忘记关闭输出。
  • 修改为 InputStream 并发送到 OutputStream ,它仍然在 output.write 处抛出一个 nullPointer。想法?
  • 你的output变量有一个null值,很奇怪。
  • 你在使用portlet吗?

标签: java jsf csv download


【解决方案1】:

3 件事在这里跳出来

  1. 未能在 FacesContext 上调用 responseComplete() 几乎意味着 JSF 将继续处理请求并且您不会影响结果。

  2. reset() 调用是不必要的。

  3. 输出流的类型应该是ServletOutputStream

    试试下面的 sn-p 代替

    try
    {
    //submitForm();
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
    
    
    response.setContentType("text/csv"); 
    fc.responseComplete();
    //response.setContentLength(contentLength); 
        response.setHeader ( "Content-disposition", "attachment; filename=\"Reporting-" + 
                new Date().getTime() + ".csv\"" );
    
    ServletOutputStream output = response.getOutputStream();
    String s = "\"Project #\",\"Project Name\",\"Product Feature(s)\",";
    s+="\"Project Status\",";
    s+="\"Install Type\",";
    s+="\"Beta Test\",\"Beta Test New/Updated\",";
    s+="\"Production\",\"Production New/Updated\",";
    s+="\n";
    InputStream is = new ByteArrayInputStream( s.getBytes("UTF-8") );
    int nextChar;
    
     while ((nextChar = is.read()) != -1) 
     {
        output.write(nextChar);
     }
        output.flush();
    
     output.close();
    
    }
    catch ( IOException e )
    {
     // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

此外,您只需拨打sos.println(s) 即可,无需您在那儿进行所有工作

【讨论】:

  • 修改后还是会报错。 com.sun.faces.lifecycle.ApplyRequestValuesPhase 执行 #{DownloadBean.doDownload}:javax.faces.el.E​​valuationException:java.lang.NullPointerException javax.faces.FacesException:#{DownloadBean.doDownload}:javax.faces.el.E​​valuationException : java.lang.NullPointerException
  • 如果我在设置后立即尝试对 response.getContentType() 执行 println,它不会返回任何内容。所以实际上我的响应是空的,因为 getContentType() 什么都不返回。响应怎么可能是空的?
  • @user1523885,如果response==null,那么response.getContentType() 将抛出一个NPE。尝试将fc.responseComplete(); 向上移动一行。
  • 没有变化。 response.getContentType() 什么也不打印,仍然抛出同样的错误。 @kolossus
  • @user1523885,为代码提供更多上下文,这是在托管 bean 中吗?这是对命令按钮action 等的响应吗?
猜你喜欢
  • 1970-01-01
  • 2011-06-01
  • 2020-03-14
  • 1970-01-01
相关资源
最近更新 更多