*
The moose likes Servlets and the fly likes Writing large files to the servletoutputstream in chunks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

Writing large files to the servletoutputstream in chunks

az ziz
Ranch Hand

Joined: Dec 17, 2008
Posts: 50
dear all,

If i have to download a large file, say > 200 mb, from the servlet if i use fileinputsream to read and write to the servletoutputstream i get outofmemory error. how do i split this file into various chunks and write them to the stream?
does using
response.setContentType("application/octet-stream");
solve the problem?
although i tried
outputStream = new GZIPOutputStream(response.getOutputStream(), 10240);
it didnt solve the problem.

thanks
V
az ziz
Ranch Hand

Joined: Dec 17, 2008
Posts: 50
can this be solved using the multi-part response? how about the browser compatibility? i read somewhere long back that IE do not understand this response ?

V
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Without knowing how you're copying from one stream to the other it's impossible to help.
az ziz
Ranch Hand

Joined: Dec 17, 2008
Posts: 50
i have few pdf files in the file server. This pdf may vary in sizes. i need to let the client download them though the servlet. i receive the file name from the request and read the file using FileInputStream. i write them to the ServletOutputStream.
during this operation i receive OutOfMemoryError.

kindly reply for any other clarification.

V
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

When someone on a Java forum asks about "how you're doing something" it's almost *always* a request for code, or at the *very* least, pseudo-code. Copying a file should use essentially zero memory unless you're doing it wrong. Read into a buffer, write the buffer out, repeat until done. No memory other than the buffer is being used.

Personally, I almost always just use something from commons-io, but it does the exact same thing.

So again, without knowing what you're doing, it's impossible to know what you might be doing wrong.
az ziz
Ranch Hand

Joined: Dec 17, 2008
Posts: 50
for your reference, please find the below codes that does the said work.

unnecessary codes are removed

pdfPrintList is the List containing the FileInputStream of the files to be sent to the client.



below code will join the list of pdfs, this will use the itext lib.



thanks
V
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Looks to me like it is the PDF combining that uses all the memory, not the servlet output stream.

Didn't you already write about this problem in the performance forum?

HERE!

WHY in the name of Alan Turing are you coming back with the same problem, after NOT taking the advice in the performance forum.

Bill
 
 
subject: Writing large files to the servletoutputstream in chunks