2017-10-04 97 views
0

我可以生成PDF並將其刷新到瀏覽器。但是,現在我的要求發生了變化。我需要生成多個pdf並將它們保存在一個zip文件中並將其刷新到瀏覽器。我跟着這http://www.avajava.com/tutorials/lessons/how-can-i-create-a-zip-file-from-a-set-of-files.html使用Java將多個pdf文件壓縮成單個文件zip文件

但無法找到如何在我的代碼集成。這是我的代碼。任何想法將不勝感激。

for(int i = 0; i < 5 ; i++) { 
      byte[] documentBytes = TSService.generateDocument(dealKey, i); 
      String documentType = TSUtil.getDocumentType(i); 
      response.setHeader("Content-Disposition", "attachment;filename="+documentType); 
      response.setContentType("application/pdf"); 
      response.setHeader("Expires", "0"); 
      response.setHeader("Cache-Control", "must-revalidate, postcheck=0, pre-check=0"); 
      response.setHeader("Pragma", "public"); 
      response.setContentLength(documentBytes.length); 
      ServletOutputStream out = response.getOutputStream(); 
      out.write(documentBytes);  
      out.flush(); 
      out.close(); 
     } 

最初我只有在循環中的代碼。現在,我想根據i值生成5個報告。亞歷克斯

String documentType = TSUtil.getDocumentType(Integer.valueOf(documentKey)); 
     response.setHeader("Content-Disposition", "attachment;filename=dd.zip"); 
     response.setContentType("application/zip"); 
     response.setHeader("Expires", "0"); 
     response.setHeader("Cache-Control", "must-revalidate, postcheck=0, pre-check=0"); 
     response.setHeader("Pragma", "public"); 

     ServletOutputStream out = response.getOutputStream(); 
     ZipOutputStream zout = new ZipOutputStream(out); 

     for(int i = 1; i <= 5 ; i++) { 
      byte[] documentBytes = TSService.generateDocument(dealKey, i); 
      ZipEntry zip = new ZipEntry(i+".pdf"); 
      zout.putNextEntry(zip); 
      zout.write(documentBytes); 
      zout.closeEntry(); 
     } 

     zout.close(); 
+0

你在這個outpustream使用在一個ZipOutputStream,沖洗所有的PDF格式,那麼結果在瀏覽器中刷新 – Tuco

+0

@Tuco,你能PLZ發佈示例代碼段? – Yakhoob

回答

2

下面的代碼應該可以工作,並且可以直接下載而不需要創建任何臨時文件。所有這些都是即時創建的,並在內存中。

response.setHeader("Content-Disposition", "attachment;filename="+***Your zip filename***); 
response.setContentType("application/zip"); 
response.setHeader("Expires", "0"); 
response.setHeader("Cache-Control", "must-revalidate, postcheck=0, pre-check=0"); 
response.setHeader("Pragma", "public"); 

ServletOutputStream out = response.getOutputStream(); 
ZipOutputStream zout = new ZipOutputStream(out); 

for(int i = 0; i < 5 ; i++) { 
    byte[] documentBytes = TSService.generateDocument(dealKey, i); 
    ZipEntry zip = new ZipEntry(***your pdf name***); 
    zout.putNextEntry(zip); 
    zout.write(documentBytes); 
    zout.closeEntry(); 
} 

zout.close(); 

修訂

我剛纔想下面的代碼,並沒有問題。一個新的zip文件可以用5個文本文件創建。所以我不知道你爲什麼會得到例外。

FileOutputStream out = new FileOutputStream("abc.zip"); 
    ZipOutputStream zout = new ZipOutputStream(out); 

    for(int i = 0; i < 5 ; i++) { 
     byte[] documentBytes = "12345".getBytes(); 
     ZipEntry zip = new ZipEntry(i+".txt"); 
     zout.putNextEntry(zip); 
     zout.write(documentBytes); 
     zout.closeEntry(); 
    } 

    zout.close(); 
+0

讓我檢查並回復你。 – Yakhoob

+0

它在ZipEntry zip = new ZipEntry(「dd.zip」); as java.util.zip.ZipException:重複項:dd.zip – Yakhoob

+0

ZipEntry用於您的PDF文件名。所以你可以將你的第一個文件命名爲1.pdf,將第二個命名爲2。pdf等等。 – Alex

1

更新代碼,我做了同樣的工作與XML file.below是我的代碼

public String createZipFromXmlFile(List<String> filePath) { 
    String date = new SimpleDateFormat("yyyMMddHHmmssSS").format(new Date()); 
    String fName = "zipDownload" + date + ".zip"; 
    System.out.println(" File Path.................."+filePath); 
    String fileName = filePath.get(0).substring(0, filePath.get(0).indexOf("xml")) + "//" + fName; 
    try { 
     FileOutputStream fout = new FileOutputStream(fileName); 
     ZipOutputStream zout = new ZipOutputStream(fout); 
     for (String fnm : filePath) { 
      FileInputStream fin = new FileInputStream(new File(fnm)); 
      ZipEntry zip = new ZipEntry(fnm.substring(fnm.indexOf("xml"))); 
      zout.putNextEntry(zip); 
      byte[] bytes = new byte[1024]; 
      int length; 
      while ((length = fin.read(bytes)) >= 0) { 
       zout.write(bytes, 0, length); 
      } 
      zout.closeEntry(); 
      fin.close(); 
     } 
     zout.close(); 
     fout.close(); 
    } catch (Exception e) { 
    } 
    return fileName; 
} 

其中,filepath是包含XML文件路徑列表。

+0

我將不會有文件路徑。該文件最初不會被存儲。我想用響應生成的pdf生成zip文件夾。 – Yakhoob

+0

您應該將pdf文件的路徑保存到列表中並傳入上面的method.my xml文件,這些文件也是在runtime.i文件中存儲到項目本地路徑中,然後獲取路徑列表。 –

+0

文件路徑將查找存儲的文件是否正確? – Yakhoob

相關問題