15

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.

Ghosh
  • 191
  • 1
  • 2
  • 5
  • 4
    You probably shouldn't send such big files through a servlet. Have you tried not setting `Content-Length` and sending your file using [Chunked transfer encoding](http://en.wikipedia.org/wiki/Chunked_transfer_encoding)? – Piotr Praszmo Jul 13 '12 at 08:41
  • Others have tried : http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4187336 Servlet API does not permits it. Have you tried sending it it anyway without Content-Length? – nomoa Jul 13 '12 at 08:43

4 Answers4

11

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));
}
LJRKUMAR
  • 325
  • 3
  • 7
  • Have you actually tried this, or are you just guessing? – user207421 Sep 03 '14 at 07:21
  • @EJP, the header name is correct "Content-Length" and there is no size restriction in the [w3c ref](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html), so what do you mean? That some browsers cannot handle long content lengths - as in the comment of Yura's answer? – Joop Eggen Sep 03 '14 at 07:37
5

Try this:

long length = ...;
response.setHeader("Content-Length", String.valueOf(length))

Hope this helps...

Yuriy Nakonechnyy
  • 3,742
  • 4
  • 29
  • 41
  • 1
    Actually that i have also tried. But it is seting content length with 0 whenever long value is coming.. – Ghosh Jul 13 '12 at 11:04
  • That's strange - could you please try `response.setHeader("Content-Length", String.valueOf(1048576))` and check HTTP response again? – Yuriy Nakonechnyy Jul 13 '12 at 11:35
  • Great, this works in Tomcat7. setContentLength((int)len) broke my 206-partialcontent video streaming for VLC player. Value as string did the trick. I don't have any pre+postprocess filters on a servlet url if that makes any difference. – Whome Sep 23 '14 at 11:54
0

Don't set it at all.

Just let it use chunked transfer mode, which is the default. There is no Content-Length header in that circumstance. See @BalusC's comment under this question.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483
0

Do not set it and use chunked encoding. Also beware of HEAD requests = these requests should return the same content-length as GET, but not sending the actual body. Default implementation of HEAD in javax.servlet.http.HttpServlet is done by calling GET on the same URL and ignoring all the response body written (only counting characters) - see the following fragment:

protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    NoBodyResponse response = new NoBodyResponse(resp); // mock response (not writing)
    doGet(req, response); // performs a normal GET request
    response.setContentLength(); // this uses INTEGER counter only
}

The problem is that the content length counter is also integer. So I recommend overloading doHead method also and not setting the content length at all (you might want to leave GET call also to save time generating the giant file).

voho
  • 2,805
  • 1
  • 21
  • 26