Thursday, May 5, 2011

Jsp download file size.

We are running tomcat, and we are generating pdf files on the fly. I do not have the file size before hand, so I cannot direcly link to a file on the server. So I directly send the output.

response.setContentType("application/force-download");
OutputStream o = response.getOutputStream();

And then I directly output to this OutputStream.

The only problem is that the receiver does not get the filesize, so they do not know how long the download will take. Is there a way to tell the response how large the file is?

EDIT I do know the filesize, I just cant tell the STREAM how big the file is.

From stackoverflow
  • Serialize the PDF byte stream to a file or in a byte array calculate its size, set the size and write it to the output stream.

    Milhous : I do have the pdf as a file. I need to tell the STREAM how big the file is.
    William Brendel : That's very inefficient, especially for large PDF files, and if notifying users of the remaining download time is a concern, chances are the file is large.
    Milhous : Well, it is just a pdf that can be from 2-56MB.
  • I beleive you're ansering the qustion your self: quote: I do not have the file size before hand, so I directly send the output.

    If you don't have the size you cant send it....

    Milhous : the size is not static, but I do know it when i send the page.
  • Why not generate the PDF file in to temp file system , or ram-base file system or memory-map file on the fly. then you can get the file size.

    response.setContentType("application/force-download"); response.setContentLength(sizeHere); OutputStream o = response.getOutputStream();

    Milhous : I have the filesize. See my Edit.
  • The response object should have a setContentLength method:

    // Assumes response is a ServletResponse
    response.setContentLength(sizeHere);
    
    William Brendel : This is the correct answer. The OP updated the question and said he knows the file size, just doesn't know how to specify that in the response.

0 comments:

Post a Comment