jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes Uploading Files problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Uploading Files problem" Watch "Uploading Files problem" New topic

Uploading Files problem

J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

hello people, i have a servlet that uploads files to a specific directory.. it works fine for small sized files..like pictures, mp3s etc.. but when i want to upload some video files or any software files i get this error :
HTTP Status 500 -

type Exception report


description The server encountered an internal error () that prevented it from fulfilling this request.


javax.servlet.ServletException: Servlet execution threw an exception
root cause

java.lang.OutOfMemoryError: Java heap space
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.26 logs.

Apache Tomcat/6.0.26

the servlet that uploads file is below :

Please help me in uploading bigger size files...
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Well, the basic problem with your code is that you read the entire uploaded file into memory (that byte array at line 48). And then you copy that into a String (at line 55), which takes up twice as much memory as that byte array (one char == two bytes). So a big enough upload file will cause your application to run out of memory and crash.

So you have to rewrite so that you don't have the whole upload in memory all at once. I can't tell you exactly how to do that, because I can't exactly tell what the code starting at line 59 is trying to do. But somehow you have to start reading the data and doing that logic as you read the first few lines. It looks like the code is supposed to only write out a subset of the upload, and that subset is specified by header data in the upload? So once you have extracted the information from the header you would carry on reading, and only write the specified data to the output file.

The important part of that is that you should only read a small amount of data at any one time.
I agree. Here's the link: http://aspose.com/file-tools
subject: Uploading Files problem
jQuery in Action, 3rd edition