2015-05-09 37 views
1

我嘗試使用Zip4j生成zip文件進行下載。 但我總是得到錯誤:調用zout.putNextEntry(文件,NULL)時使用Zip4j生成Zip下載

2015-05-09 15:56:24.306 ERROR 11748 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause

;下面

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功能,即使沒有得到迴應,但媒體鏈接獲得的流?

回答

2

那是因爲,線

zout.putNextEntry(file, null); 

是投擲因爲空參數的空指針。而且,由於我沒有看到你的servlet的其餘部分,我的猜測是,你的servlet可能試圖再次獲取輸出流來處理/拋出這個異常。

上述putNextEntry()調用中的第二個參數是ZipParameters。正如名稱所描述的,這個參數指定了各種zip參數,例如,如果zip是受密碼保護的,或者zip的內容是從文件或輸入流中讀取的,等等。這是一個必需的參數。

該調用的第一個參數是一個File對象。這僅在您從本地文件流構建壓縮文件時才需要。如果您從外部流(例如您的情況)構建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(); 
} 
+0

感謝,現在的作品, 但我有另一種方法,其中參數不爲null,但: ZipParam eters parameters = new ZipParameters(); parameters.setEncryptFiles(true); parameters.setPassword(「AMOS」); 我已經添加了新參數: parameters.setSourceExternalStream(true); parameters.setFileNameInZip(「employee.txt」); 但它仍然無法正常工作,對此有何猜測? – Matombo

1

如果有人需要郵編使用Zip4j庫密碼加密的文件,這裏是一個代碼(如提及here):

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); 
} 

希望我」 ve幫了別人...