【发布时间】: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吗?