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