繁体   English   中英

使用Zip4j生成Zip下载

[英]Generating Zip for Download using Zip4j

我尝试使用Zip4j生成一个zip文件供下载。 但我总是得到错误:

2015-05-09 15:56:24.306 ERROR 11748 --- [nio-8080-exec-4] oaccC [。[。[/]。[dispatcherServlet]:servlet [dispatcherServlet]的Servlet.service()与上下文path []引发异常[请求处理失败; 嵌套异常是java.lang.IllegalStateException:已根据原因为此响应调用了getOutputStream()

调用zout.putNextEntry(file,null)时; 在下面的功能中

public void EmployeeEncyrptedZipFileDownload(HttpServletResponse response, @RequestParam(value = "id", required = true) int employeeId) throws IOException, ZipException
{
    //Prepare text file contents
    String fileContent = "Hallo Welt";

    response.setContentType("application/zip");
    response.setHeader("Content-Disposition", "attachment;filename=test.zip");

    final StringBuilder sb = new StringBuilder(fileContent);
    final ZipOutputStream zout = new ZipOutputStream(response.getOutputStream());

    File file = new File("mytext.txt");
    zout.putNextEntry(file, null);
    byte[] data = sb.toString().getBytes();
    zout.write(data, 0, data.length);

    zout.closeEntry();
    zout.finish();
}

怎么可能,因为putNextEntry函数甚至没有获得响应但是已经获得了全部流?

那是因为,这条线

zout.putNextEntry(file, null);

因为null参数而抛出空指针。 而且,由于我没有看到你的servlet的其余部分,你的servlet可能会尝试再次获取输出流以处理/抛出此异常。

上面的putNextEntry()调用中的第二个参数是ZipParameters。 如名称所述,此参数指定各种zip参数,例如,如果zip受密码保护,或者zip的内容是从文件或输入流中读取的,等等。这是必需的参数。

此调用的第一个参数是File对象。 只有在从本地文件流构建zip时才需要这样做。 如果要从外部流构建zip(例如在您的情况下),则此参数可以为null。 我知道这不是一个好的设计,将在即将发布的版本中修复。

修复您的方案是:

public void EmployeeEncyrptedZipFileDownload(HttpServletResponse response, @RequestParam(value = "id", required = true) int employeeId) throws IOException, ZipException
{
    //Prepare text file contents
    String fileContent = "Hallo Welt";

    response.setContentType("application/zip");
    response.setHeader("Content-Disposition", "attachment;filename=test.zip");

    final StringBuilder sb = new StringBuilder(fileContent);
    final ZipOutputStream zout = new ZipOutputStream(response.getOutputStream());

    ZipParameters zipParameters = new ZipParameters();
    zipParameters.setSourceExternalStream(true);
    zipParameters.setFileNameInZip("mytext.txt");

    zout.putNextEntry(null, zipParameters);
    byte[] data = sb.toString().getBytes();
    zout.write(data, 0, data.length);

    zout.closeEntry();
    zout.finish();
}

如果有人需要使用邮编库Zip4j用密码加密的文件,这里是一个代码(如提到这里 ):

public void zipFileWithPassword(String fileToZipPath,String password,String zippedFilePath) throws ZipException
{
    ZipFile zipFile=new ZipFile(zippedFilePath);
    ZipParameters parameters=new ZipParameters();
    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
    parameters.setEncryptFiles(true);
    parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
    parameters.setPassword(password);
    File fileToZip=new File(fileToZipPath);
    log(Severity.INFO,"Creating a ZIP file: %s",fileToZipPath);
    zipFile.addFile(fileToZip,parameters);
}

希望我帮助过某个人......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM