How do I return a zip file to the browser via the response OutputStream?
Asked Answered
B

4

10

In this situation, I have created a zip file containing search result files, and am trying to send it to the user. Here is the chunk of code I am currently trying to use.

File[] zippable = new File[files.size()];
File resultFile = ZipCreator.zip(files.toArray(zippable), results);
InputStream result = new FileInputStream(resultFile);
IOUtils.copy(result, response.getOutputStream());

However, this currently doesn't work quite right. Instead of returning the zip file that I have created, it returns an html file. If I manually change the file extension afterwards, I can see that the contents of the file are still the search results that I need. So the problem just lies in returning the proper extension to the response.

Does anyone have any advice for this situation?

Borecole answered 11/1, 2012 at 16:45 Comment(0)
B
9

You need to set the Content-Type response header to the value application/zip (or application/octet-stream, depending on the target browser). Additionally, you may want to send additional response headers indicating attachment status and filename.

Boote answered 11/1, 2012 at 16:48 Comment(4)
I think I may be misinterpreting you here. I've added a line to the code, so it now reads response.setHeader("Content-Type", "application/zip"); Still no success though. This call is being made just before the call to IOUtils.copyBorecole
@Bennie: looks good, but again, there is some confusion about the "right" MIME type to use for a ZIP file, so you might want to play with those values.Boote
Alright, it looks like it's working now, at least for returning the file. It's doing a strange thing though, where the file is called "exportZipSearchResults.htm.zip". Is there a built-in way to change the filename on the response?Borecole
@Bennie: try using the Content-Disposition header (possibly with value "inline") and a filename of your choice.Boote
O
2

You need to set the content type header to application/octet-stream prior to streaming the results. Depends on what implementation of response you are using on how you actually do this.

Obstinate answered 11/1, 2012 at 16:48 Comment(0)
W
1

Here is some working code, just in case anyone needs it:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {

        // The zip file you want to download
        File zipFile = new File(zipsResourcesPath + zipFileName);

        response.setContentType("application/zip");
        response.addHeader("Content-Disposition", "attachment; filename=" + zipFileName);
        response.setContentLength((int) zipFile.length());

        try {

            FileInputStream fileInputStream = new FileInputStream(zipFile);
            OutputStream responseOutputStream = response.getOutputStream();
            int bytes;
            while ((bytes = fileInputStream.read()) != -1) {
                responseOutputStream.write(bytes);
            }
        } catch (IOException e) {
            logger.error("Exception: " + e);
        }
}

And the HTML:

<a class="btn" href="/path_to_servlet" target="_blank">Download zip</a>

Hope this helps!

Whoa answered 15/5, 2018 at 12:32 Comment(3)
Please show how you defined the "zipFile" ? How you added files into this "zipFile"?Cichlid
I've edited the code so you can see how the zipFile is defined. Please reconsider the downvote @Cichlid . I appreciate comments, as this helps me to improve my answers.Whoa
this is getting already created zip file. But how to get multiple files and make them into zip using Java? and then send to response.Cichlid
P
-1

So I found a hack for this : ) Just add ".zip" in your filename and set your content type as application/zip. Works like a charm.

response.setContentType("application/zip");
String licenseFileName = eId;
response.setHeader("Content-disposition", "attachment; filename=\"" + licenseFileName +".zip");
Podophyllin answered 20/7, 2021 at 8:50 Comment(2)
what is eId in this example?Albata
@Albata eId was an identifier in my case. It's just the file name you want. It can be anything basically.Podophyllin

© 2022 - 2024 — McMap. All rights reserved.