views:

604

answers:

4

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.

+1  A: 

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.

Boris Pavlović
I do have the pdf as a file. I need to tell the STREAM how big the file is.
Milhous
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.
William Brendel
Well, it is just a pdf that can be from 2-56MB.
Milhous
A: 

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....

Tomas
the size is not static, but I do know it when i send the page.
Milhous
A: 

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();

ariso
I have the filesize. See my Edit.
Milhous
+5  A: 

The response object should have a setContentLength method:

// Assumes response is a ServletResponse
response.setContentLength(sizeHere);
R. Bemrose
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.
William Brendel