Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My FileUploadServlet not uploading a file if its size exceeds 1KB

 
Srinivasa Maddi
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I wrote a servlet which will upload a text file but failing if the file size exceeds 1KB.


When i debugged, i found its failing in the while loop. Can one one tel me where i was going wrong.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"new String(dataBytes)" is almost certainly incorrect. Files are binary data - they can't be converted to a string. And even if they are text, this call disregards the question of which encoding the file contents are in.

But more importantly, is there a particular reason you're trying to reinvent the wheel? HTTP file upload is rather complex, and has been comprehensively solved by libraries like Apache Commons FileUpload.
 
Srinivasa Maddi
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reason being, my requirement is demanding me not to go with any third party software though its free.
 
Rajat Raina
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can set the max size of your file more than 1KB.
The function is setSizeMax(size) but for this you have to use DiskFileUpload.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would question that requirement. It is a waste of your time to do this all over again.

But certainly you could use the source of that library (or of any other of the multiple available ones) as inspiration for your solution.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinivasa Maddi wrote:reason being, my requirement is demanding me not to go with any third party software though its free.

I really don't understand why ones always come up with such lame excuse? Where does this aversion come from?

Apache Commons FileUpload is a well developed and thoroughly tested file upload API. Honestly, with the knowledge you've shown as far, you would require and waste too much time to achieve the same robustness as Apache Commons FileUpload. That isn't worth the effort.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic