• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Returning large PDF file causing the server to crash.

Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.setHeader("Content-disposition", "inline;filename=report.pdf");

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


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.
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Posts: 25798
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic