JSF CSV中的豆豆下载 [英] JSF CSV download in bean

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

问题描述

我正在尝试以与以下相同的方式进行csv下载:
如何从JSF备份bean中提供文件下载?

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

我的回复不断地抛出一个 nullPointerException output.write()行。 bean是请求范围。任何关于null指针的想法?

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.

reset()调用是不必要的。

输出流应为类型为 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中的豆豆下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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