File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Websphere and the fly likes Returning large PDF file causing the server to crash. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Websphere
Bookmark "Returning large PDF file causing the server to crash." Watch "Returning large PDF file causing the server to crash." New topic
Author

Returning large PDF file causing the server to crash.

Anurag Narayan
Ranch Hand

Joined: Jul 14, 2008
Posts: 41
Hi,
In my enterprise application I'm trying to generate a heavy PDF(around 800MB) and present to client for printing. The problem is that such a huge file is causing Websphere server to crash causing OutOfMemory Error.

Follwoing is the method which is returning the data to browser using response object.


* Utility method that demonstrates how to write an input stream to the server's local file system.
*/
private void writeToBrowser(ByteArrayInputStream byteArrayInputStream, HttpServletResponse response, String mimetype) throws Exception {

//Create a byte[] the same size as the exported ByteArrayInputStream.
byte[] buffer = new byte[byteArrayInputStream.available()];
int bytesRead = 0;

//Set response headers to indicate mime type and inline file.
response.reset();
response.setHeader("Content-disposition", "inline;filename=report.pdf");
response.setContentType(mimetype);

//Stream the byte array to the client.
while((bytesRead = byteArrayInputStream.read(buffer)) != -1) {
response.getOutputStream().write(buffer, 0, bytesRead);
}

//Flush and close the output stream.
response.getOutputStream().flush();
response.getOutputStream().close();

}


I think that the resp.getOutputStream().write is being stored in memory until the data can be sent through to the client. So the entire file might be read and stored in the resp.getOutputStream() causing memory issues and crashing!

I have tried Buffering these streams and also tried using Channels from java.nio, none of which seems to make any bit of difference to my memory issues. I have also flushed the outputstream once per iteration of the loop and after the loop, which didn't help.

Anurag Narayan<br />SCJP5,SCEA(OCMJEA)
Deepak Pant
Ranch Hand

Joined: Feb 13, 2004
Posts: 445
I think it is a bad idea to stream 800MB of file using App/Web Server. You could instead generate the file and make it available via FTP server for printing etc.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

If you're putting the entire file in a byte array in memory, it isn't surprising that it crashes the server. You shouldn't have to do that, so change your code so that you don't do it.

And yes, it's your code which is doing that. Not only that, but you are doing it twice. Trying to blame something else for keeping another copy isn't going to get you off the hook.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning large PDF file causing the server to crash.