Reputation: 4716
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?
Upvotes: 14
Views: 13797
Reputation: 1108802
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());
Upvotes: 14
Reputation: 597124
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.
Upvotes: 10