我正在尝试创建一个zip文件,并在点击jsf中的链接时下载该文件。 我无法查看创建的zip文件,也无法下载它。我在后端获得了以下异常以及 nullpointer异常。 java.lang.IllegalStateException:Servlet响应已使用流,Writer不可能
请仔细阅读以下代码,并在这方面为我提供帮助。
//I am calling downloadZip2() method on click of the command link in jsf
public void downloadZip2() {
System.out.println("downloadZip.....................");
crgAttList = nameDTO.getAttachmentsList();
// File zipping implementation
String directoryPath ="G:/attachment";
logger.info("Directory-Path===" + directoryPath);
File directoryToZip = new File(directoryPath);
ArrayList<String> attachmentsList = new ArrayList<String>();
for (int i = 0; i < crgAttList .size(); i++)
{
attachmentsList.add(crgAttList.get(i).getAttachmentNameWithPath());
}
if (attachmentsList != null && attachmentsList.size() > 0) {
logger.info("---Creating zip file");
logger.info("Attachment-List-Size=====>" + attachmentsList.size());
zipAttachments(attachmentsList, directoryPath + ".zip");
}
}
public void zipAttachments(List<String> attachmentsList, String zipFileName) {
byte[] buf = null;
ZipOutputStream out = null;
FileInputStream in = null;
OutputStream os = null;
try {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
response.setContentType("application/zip");
response.setHeader("Content-Disposition",
"attachment;filename=attachment.zip");
System.out.println(" in zipAttachments before zipFile creation");
buf = new byte[1024];
os = new FileOutputStream(zipFileName);
out = new ZipOutputStream(os);
out = new ZipOutputStream(response.getOutputStream());
for (int i = 0; i < attachmentsList.size(); i++) {
System.out.println(" in zipAttachments for before adding :: "
+ attachmentsList.get(i) + " file to zip");
in = new FileInputStream((String) attachmentsList.get(i));
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(attachmentsList.get(i).substring(
attachmentsList.get(i).indexOf(".") + 1)));
// Transfer bytes from the file to the ZIP file
int len = 0;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
in.close();
out.flush();
out.closeEntry();
System.out.println(" in zipAttachments for:: "
+ attachmentsList.get(i) + " file is added to zip");
}
System.out.println(" in zipAttachments after zipFile creation");
System.out.println(" in zipAttachments after out.close");
out.close();
} catch (Exception e) {
System.out.println(" in zipAttachments error" + e);
e.printStackTrace();
}
}
例外
java.lang.IllegalStateException: Servlet response already use stream, Writer not possible
at org.ajax4jsf.webapp.FilterServletResponseWrapper.getWriter(FilterServletResponseWrapper.java:207)
at com.sun.facelets.FaceletViewHandler.createResponseWriter(FaceletViewHandler.java:400)
at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:557)
at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:109)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:341)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
javax.servlet.ServletException
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:277)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:341)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
Caused by: java.lang.NullPointerException
at com.sun.facelets.util.DevTools.writeVariables(DevTools.java:168)
at com.sun.facelets.util.DevTools.writeVariables(DevTools.java:145)
at com.sun.facelets.util.DevTools.debugHtml(DevTools.java:109)
at com.sun.facelets.FaceletViewHandler.handleRenderException(FaceletViewHandler.java:677)
at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:646)
at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:109)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
答案 0 :(得分:1)
使用
FacesContext.getCurrentInstance().responseComplete();
在你向它写入数据后,阻止JSF试图搞砸响应。