JSF CSV下载在bean中 [英] JSF CSV download in bean

查看:195
本文介绍了JSF CSV下载在bean中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试以与这里相同的方式执行csv下载:
如何在JSF备份bean中下载文件?

I am trying to do a csv download in the same fashion as here: How to stream a file download in a JSF backing bean?

我的响应不断地抛出 nullPointerException output.write()行。 bean是请求范围的。关于空指针的任何想法?

My response keeps throwing a nullPointerException at the output.write() line. The bean is of the request scope. Any thoughts as to the null pointer?

    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();
    }


推荐答案

/ p>

3 things jump out here


  1. 无法呼叫 responseComplete() FacesContext 几乎意味着JSF将继续处理请求,并且不会影响结果。

  1. Failing to call responseComplete() on FacesContext pretty much means JSF will continue to process the request and you don't get to affect the outcome.

c $ c> reset()调用是不必要的。

The reset() call is unnecessary.

输出流类型应为 ServletOutputStream

尝试以下代码段

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),而不需要你正在做的所有工作

Additionally, you can just call sos.println(s) without the need for all that work you're doing there

这篇关于JSF CSV下载在bean中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆