使用Java将多个pdf压缩为单个文件zip文件 [英] Zip multiple pdf into a single file zip file using Java
问题描述
我能够生成pdf并将其刷新到浏览器.但是,现在我的要求已更改.我需要生成多个pdf并将它们保存在一个zip文件中,然后将其刷新到浏览器中.我遵循了这个 http://www.avajava.com/tutorials/lessons/how-can-i-create-a-zip-file-from-a-set-of-files.html >
但是找不到如何集成到我的代码中.这是我的代码.任何想法将不胜感激.
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个报告.
Alex的更新代码
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();
下面的代码应该可以使用,并且可以直接下载而无需创建任何临时文件.所有这些都是动态创建的,并且在内存中.
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();
已更新
我刚刚尝试了以下代码,没有任何问题.可以使用5个文本文件创建一个新的zip文件.所以我不知道为什么会有例外.
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();
I'm able to generate a pdf and flush it to the browser. But, now my requirement is changed. I need to generate multiple pdf and keep them in a single zip file and flush it to the browser. I followed this http://www.avajava.com/tutorials/lessons/how-can-i-create-a-zip-file-from-a-set-of-files.html
But could not find how to integrate in my code. Here is my code. Any ideas would be greatly appreciated.
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();
}
Initially I had only code which is in loop. Now, I want to generate 5 reports based on i value.
Updated code for Alex
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();
Below code should be worked and can be directly downloaded without creating any temp files. All are created on the fly and are on memory.
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();
UPDATED
I have just tried the below code and without problem. A new zip file can be created with 5 text files inside. So I have no idea why you get exceptions.
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();
这篇关于使用Java将多个pdf压缩为单个文件zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!