使用 Zk 框架下载 zip

download zip with Zk framework

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

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 class 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,这将失败,因为您在 worker 中调用 save()线程(即无法访问 Desktop 的线程)。

阅读此 link 了解有关如何在 Executions.activate(Desktop) 的帮助下从工作线程访问 desktop/execution 的更多信息: https://www.zkoss.org/wiki/Small_Talks/2015/January/Simplify_Long_Operation_Handlings#Long_Operations_made_simple