Generate Zip File - download PDF from Url and Generate Zip to Download in Browser - Broken PDF

512 Views Asked by At

I have create Rest Service and I am trying to Generate Zip file. This Zip file created from muliple PDF files which are downloaded using method InputStream inpuStream = new URL(url).openStream() . I am able to Generate Zip file Which included PDF files but PDF files are broken. Even If i try to Generate it from String its coming as broken PDF and i am getting Error message "Not a supported File Type or file is broken or damaged". Its simple code but seems like i am unable to track the mistake. I have provided my controller , service method for your reference. 1)Controller:

 @GetMapping("/getZipFile")
public void getZipFile(HttpServletResponse response) throws RestException {
    try {
        ByteArrayOutputStream baos = generateZipService.getZipFile();
        ServletOutputStream responseOutPutStream = response.getOutputStream();
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setStatus(HttpServletResponse.SC_OK);
        response.addHeader("Content-Disposition", "attachment; filename=\"GeneratedZipFile.zip\"");
        responseOutPutStream.write(baos.toByteArray());
        responseOutPutStream.flush();
    } catch (Exception e) {
        throw new RestException("Error In downloading Zip File");
    }

}

2)Service Method

  public ByteArrayOutputStream getZipFile() throws Exception{
     List<ZipFileName> zipFileNames=     zipFileNameDao.getZipFileName();
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       ZipOutputStream zipOut= new ZipOutputStream(baos);
       for (String fileName : zipFileNames) {
             InputStream inpuStream = new ByteArrayInputStream( "this is test to generarte pdf test file this is test tdfsfs this is test to generarte pdf test file this is test tdfsfs".getBytes(Charsets.UTF_8) );
             createZipFile(inpuStream,zipOut,fileName);
             inpuStream.close();
         }
        zipOut.flush();
        baos.flush();
     
        zipOut.close();
        baos.close();
        return baos;
 }

3)createzipfile from service method :

```private void createZipFile(InputStream inputStream, ZipOutputStream zipOut,String fileName) throws IOException {
     
     ZipEntry zipEntry = new ZipEntry(fileName+".pdf");
     BufferedInputStream bis = new BufferedInputStream(inputStream);
     
     zipOut.putNextEntry(zipEntry);
     zipOut.write(IOUtils.toByteArray(inputStream));
    
     zipOut.closeEntry();
     bis.close();
     inputStream.close();
 }

Also , Another question is about using channels. I read channels are better when you have large files to downlaod from server . I have less then 20 kb of file so should I use Java.nio or just Zipoutputstream is fine.

I try with "response.setContentType("APPLICATION/ZIP")" but it didnt change the outcome of the project. Thank you for your help..

1

There are 1 best solutions below

0
On

The code worked fine only thing missing was to pass authentication with the openStream() method because of which I was getting the broken PDF. I opened the pdf with notepad++ and found the error .. I resolved it. Thank you