• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Uploading Files problem

 
Ranch Hand
Posts: 83
MS IE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

message

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

exception

javax.servlet.ServletException: Servlet execution threw an exception
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
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...
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic