I am writing a web server in java that is transferring file upto 2GB fine. When I searched for the reason, I found like java HttpServelet only allows us to set the content length as int. As the maximum size of integer is 2GB, its working fine upto 2gb when I am using response.setContentLength method. Now the problem is bydefault response.setContentLength has the parameter of integer. So it is not taking long value as parameter. I have already tried response.setHeader("Content-Length", Long.toString(f.length())); response.addHeader("Content-Length", Long.toString(f.length())); but nothing is working. All time it is failing to add content length when it is a long value. So please give any working solution for HTTPServletResponse so that I can set the content length as long value.
To manually pass the Content-Length header, you need to add the Content-Length: [length] and Content-Type: [mime type] headers to your request, which describe the size and type of data in the body of the POST request.
To check this Content-Length in action go to Inspect Element -> Network check the request header for Content-Length like below, Content-Length is highlighted.
The Content-Length header is mandatory for messages with entity bodies, unless the message is transported using chunked encoding. Content-Length is needed to detect premature message truncation when servers crash and to properly segment messages that share a persistent connection.
The Content-Length header indicates the size of the message body, in bytes, sent to the recipient.
You can also use below sample code.
long length = fileObj.length();
if (length <= Integer.MAX_VALUE)
{
response.setContentLength((int)length);
}
else
{
response.addHeader("Content-Length", Long.toString(length));
}
Try this:
long length = ...;
response.setHeader("Content-Length", String.valueOf(length))
Hope this helps...
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