开发者

javax.servlet.HttpServletRequest.getContentLength() returns int only

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 gre开发者_高级运维ater 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜