2017-10-04 724 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

相关问题