Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Uploading Files problem  RSS feed

J Das
Ranch Hand
Posts: 83
Firefox Browser Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello people, i have a servlet that uploads files to a specific directory.. it works fine for small sized 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
Posts: 22374
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!