Reputation: 29
Today, I try to write code for downloading zip file. However, I found a tricky issue?
HttpServletResponse response
...
response.setContentType("application/zip");
response.setHeader("Content-Disposition", String.format("attachment; filename=myzipfile-" + new Date().getTime() + ".zip"));
File file = new File(somePath);
InputStream inputStream = new FileInputStream(file);
IOUtils.copy(inputStream, response.getOutputStream());
inputStream.close();
response.setContentLengthLong(file.length());
response.flushBuffer();
Then, I try to download two files, the size of the first one is 232, I can download it with Content-Length:232 in the header.
However, when I download the larger one whose size is 8,392,236 bytes, I can also download it. But I failed to get the Content-Length from the response header?
How can help to see what is the problem? Is there a limit for content length in the response header?
Upvotes: 0
Views: 896
Reputation: 4477
The Content-Length information is part of the HTTP response header fields. On the TCP connection they are sent first from server to client.
Once the server started to write the entity body of the response, there's no way in the HTTP protocol for the server to transport the content length information to the client.
The difference between your two examples is that the smaller content still fits into the buffer of your servlet container's response implementation. So it can send the content length before any content byte. The response body is only sent upon the following call to flushBuffer()
Upvotes: 1