This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes My FileUploadServlet not uploading a file if its size exceeds 1KB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "My FileUploadServlet not uploading a file if its size exceeds 1KB" Watch "My FileUploadServlet not uploading a file if its size exceeds 1KB" New topic
Author

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

Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
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
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
"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.


Ping & DNS - my free Android networking tools app
Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
reason being, my requirement is demanding me not to go with any third party software though its free.
Rajat Raina
Greenhorn

Joined: Feb 17, 2009
Posts: 10
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.


To be a winner first you need to be a begineer.
Raina Rajat
SCWCD (95%)
SCJP(70%)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
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

Joined: Oct 08, 2006
Posts: 2458
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.
 
 
subject: My FileUploadServlet not uploading a file if its size exceeds 1KB
 
Similar Threads
Uploading a text file to a blob in MySql, then downloading that file to local file system
Type conversion between Bytes and Strings
moving files over the network
Not able to convert large sized byte array to String
error in uploading zip file