gpt4 book ai didi

java - java中动态压缩和下载zip文件

转载 作者:行者123 更新时间:2023-12-01 12:07:29 29 4
gpt4 key购买 nike

我正在尝试创建一个 zip 文件并通过单击 jsf 中的链接下载该文件。我无法查看创建的 zip 文件,也无法下载它。我在后端收到以下异常以及空指针异常。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)

最佳答案

使用

FacesContext.getCurrentInstance().responseComplete();

在您向 JSF 写入数据后,阻止 JSF 试图扰乱响应。

关于java - java中动态压缩和下载zip文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27478778/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com