Maxii
Maxii

Reputation: 703

Java: ServletOuputStream content length

For downloading a file, i use the following methods of HttpServletResponse

resp.setContentType("application/force-download");
resp.setHeader("Content-Disposition:", "attachment;filename=" + "\"" + name + "\"");

There is also a method to set the content length of the response. What are the advantages of this method? When should I use this method?

Upvotes: 2

Views: 299

Answers (2)

christopher
christopher

Reputation: 27346

It means the exact byte length of the HTTP body. Generally it is used for HTTP 1.1 so that the receiving party knows when the current response/request has finished, so the connection can be reused for another request. Alternatively, content-length can be omitted and a chunked encoding can be used, or if both are missing, then at the end of the response the connection must be closed.

Taken from What's the "Content-Length" field in HTTP header?.

Upvotes: 1

morgano
morgano

Reputation: 17422

Use the header Content-Length, this specifies the length of the response in bytes.

An advantage would be to make the receptor aware of the total size of the response and be able to show a progress bar in the user interface.

Upvotes: 3

Related Questions