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.
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.
I had done same task with xml file.below is my code
where filePath is list that contains path of xml files.