In order to deal with a large request body in a HTTP POST or PUT, relying on HttpServletRequest.getContentLength()
is not a good idea, since it returns only integer values, or -1 for request bodies > 2GB.
However, I see no reason, why larger request bodies should not be allowed. RFC 2616 says
Any Content-Length greater than or equal to zero is a valid value.
Is it valid to use HttpServletRequest.getHeader('content-length')
and parse it to Long instead?
Since Servlet 3.1 (Java EE 7), a new method is available, getContentLengthLong()
:
long contentLength = request.getContentLengthLong();
The same applies to its counterpart on the response, setContentLengthLong()
:
response.setContentLengthLong(file.length());
Yes, this is a fine approach - use getHeader("Content-Length")
(capitalized) and Long.parseLong(..)
. I believe it's what the container is doing, but it is limited to int
by the serlvet spec.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With