0

I have 2 different servlets, both are handling POST calls, both stream a file as the result. One always ends up with a Content-Length header, the other never does. They both use javax.servlet.http.HttpServletResponse.

Both set "Content-Type" = "application/octet-stream";

I haven't been able to figure out what I could be doing to cause this difference.

The only real difference I see in the processing is that the size of the file of the one that does NOT get a Content-Length is generally multiple MB, and the other is usually just a few KB.

Just for the heck of it I did try using the response.AddHeader("thing", size), and noticed that that header never showed up for either servlet.

Note that I'm receiving the file on an OS/X machine, and using the [NSHTTPURLResponse allHeaders] to see the response headers.

Here is some HEAVILY trimmed down code. Basically only kept anything that refd the response:

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("application/octet-stream");
    resp.setBufferSize(256 * 1024);
    OutputStream rout = resp.getOutputStream();
// lots of work getting the stream
// Also left out all the exception handling and error checking
    IOUtils.copy(datafile, rout);
    rout.close();
    resp.addIntHeader("OnDeck-Test", 555);
  }
CasaDelGato
  • 1,263
  • 1
  • 12
  • 29

1 Answers1

0

And I had a DOH! moment a few minutes later. Of course it can't set the Content-Length before the huge file is fully streamed - and since it streams in pieces, the content length just isn't known when the reply is received. And the reason my addIntHeader() doesn't work is that it was done AFTER the file was streamed, and so the response had been sent long ago.

CasaDelGato
  • 1,263
  • 1
  • 12
  • 29