1

This is my code and response just below the code.

@GetMapping(value = "/download/tutorial/{tutorialId}", produces = "application/zip")
    public void zipDownload(@PathVariable String tutorialId, HttpServletResponse response) throws IOException {
        ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
        List<TutorialFileDownload> downloads = tutorialFileService.downloadFiles(compos.getLong(tutorialId));
        for (TutorialFileDownload f : downloads) {
            FileSystemResource resource = new FileSystemResource(dropBoxService.downloadFile(f.getFilename(), f.getPathLower()));
            ZipEntry zipEntry = new ZipEntry(resource.getFilename());
            zipEntry.setSize(resource.contentLength());
            zipOut.putNextEntry(zipEntry);
            StreamUtils.copy(resource.getInputStream(), zipOut);
            zipOut.closeEntry();
        }
        zipOut.finish();
        zipOut.close();
       // response.setStatus(HttpServletResponse.SC_OK);
        response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=test.zip");
    }

This is the reponse

Response

stevecross
  • 5,588
  • 7
  • 47
  • 85
p.maimba
  • 33
  • 8
  • I don't fancy visiting a random link to somewhere else - can you not post the response here? – Boris the Spider Dec 18 '19 at 16:09
  • And what is the problem? What else do you expect as a response and why? How do you et and display that response? – JB Nizet Dec 18 '19 at 16:14
  • 6
    Note: headers must be added before writing the body. – JB Nizet Dec 18 '19 at 16:18
  • Does this answer your question? [Do I need Content-Type: application/octet-stream for file download?](https://stackoverflow.com/questions/20508788/do-i-need-content-type-application-octet-stream-for-file-download) – rkosegi Dec 18 '19 at 16:19
  • `response.addHeader` in front of the output and `produces = "application/octet-stream"` for simply downloading. Header lines are written first, then an empty line, then the content (the zip file here). Better even use `response.setHeader`. – Joop Eggen Dec 18 '19 at 16:27
  • 4
    @JBNizet's comment is key to the problem - the attachment header (content disposition) needs to be added **before** writing content. Headers come first in the response and are flushed as soon as the `OutputStream` is written to. Adding headers after this point does literally nothing. – Boris the Spider Dec 18 '19 at 16:29
  • headers must be added before writing the body. Thanks – p.maimba Dec 18 '19 at 17:02

0 Answers0