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.
解决方案
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));
}