gpt4 book ai didi

java - 使用 Zk 框架下载 zip

转载 作者:行者123 更新时间:2023-11-30 06:44:24 30 4
gpt4 key购买 nike

我正在使用 JDK 1.8、tomcat 7.0、ZK 8.0.2.2 和 jsf-2.2.8,并且我的 web-inf/lib 中有 ZK 框架 jar 的 fowlling :

zcommon-8.0.2.2.jar

zel-8.0.2.2.jar

zk-8.0.2.2.jar

zul-8.0.2.2.jar

zweb-8.0.2.2.jar

bsh-2.0b6.jar

commons-codec-1.8.jar

commons-fileupload-1.2.2.jar

javassist-3.18.2-GA.jar

slf4j-api-1.7.5.jar

slf4j-jdk14-1.7.5.jar

我成功创建了一个包含 4 个文件的 zip 文件。

这是创建 zip 文件的函数 公共(public)类 FileDownloadServer {

   public static void getZipFile(List<String> fileList, String fileZip, String sourcePath){
byte[] buffer = new byte[2014];
try {
FileOutputStream fos = new FileOutputStream(fileZip);
ZipOutputStream zos = new ZipOutputStream(fos);

for(String file : fileList){
ZipEntry ze= new ZipEntry(file);
zos.putNextEntry(ze);
System.out.println("avant");
FileInputStream in = new FileInputStream(sourcePath + File.separator + file);
System.out.println("aprés");
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
zos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}


}

这里是用户的功能:

    @ManagedBean
public class testBean {

private final static List<String> fileList=Arrays.asList("x.csv","z.csv","y.csv","t.csv");
private final static String sourcePath="C:/output/";
private final static String fileZip="C:/zip/batch.zip";
private final static String contentType="application/zip";

public void download() throws IOException{
FileDownloadServer.getZipFile(fileList, fileZip, sourcePath);

File f = new File(fileZip);
InputStream is = new FileInputStream(f);
if(is.equals(null)){
System.out.println("is is null");
}else{
System.out.println(" is not null");
}
if(contentType.equals(null)){
System.out.println("contentType is null");
}else{
System.out.println("contentType not null");
}
if(f.getName()!=null){
System.out.println("name " + f.getName());
}else{
System.out.println("flnm is null");
}

//For downloading zip file
Filedownload.save(is, contentType, f.getName());
}

}
我收到此异常 java.lang.NullPointerException并且函数的参数不为空,我尝试在调用函数之前显示它们,一切都很好:

mai 10, 2017 6:04:33 PM com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError
GRAVE: java.lang.NullPointerException
at org.zkoss.zul.Filedownload.save(Filedownload.java:54)
at org.zkoss.zul.Filedownload.save(Filedownload.java:94)
at ma.controle.gestion.bean.batch.BatchExcelBean.uploadFile(BatchExcelBean.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.el.parser.AstValue.invoke(AstValue.java:279)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at org.primefaces.component.fileupload.FileUpload.broadcast(FileUpload.java:319)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:755)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:931)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.primefaces.barcelona.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:32)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:958)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

感谢您的帮助。

谢谢。

最佳答案

只是为了将来的读者总结一下:Filedownload.save() 中失败的行尝试检索当前的 Execution,这将在您调用时失败工作线程(即无权访问桌面的线程)中的save()

阅读此链接,了解有关如何借助 Executions.activate(Desktop) 从工作线程访问桌面/执行的更多信息: https://www.zkoss.org/wiki/Small_Talks/2015/January/Simplify_Long_Operation_Handlings#Long_Operations_made_simple

关于java - 使用 Zk 框架下载 zip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43899831/

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