aspose file tools*
The moose likes Servlets and the fly likes Sample for a servlet handling file upload? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sample for a servlet handling file upload?" Watch "Sample for a servlet handling file upload?" New topic
Author

Sample for a servlet handling file upload?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I'm pretty new to Java and Servlets (but "old" in HTML/HTTP Servers etc). I need a servlet, that is able to process a HTTP Post that includes a multipart MIME content a.k.a File-Upload with multiple files.
What I want to do:
- check for a "legal" file extension
(e.g. only loading gif, jpg, png)
- check for the sizes of the files
(e.g. only upload up to 20 kb)
- save the file to disk.
The problems i'm clueless about:
- how to find the files within the request body
- how to determine the size upfront
- how to decode the files back to the binary format
THX
stw
Shubhrajit Chatterjee
Ranch Hand

Joined: Aug 23, 2001
Posts: 356
Use multipart servlet .. for example you can look at the following package ...
com.oreilly.servlet
For more info visit the following link
http://www.servlets.com/cos/index.html


Shubhrajit
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Stephan, I'm using jspsmartupload to achieve all the things you want :-).
check www.jspsmart.com
if you need any help drop me an email..
britt50423@yahoo.com
good luck


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
On the client side, the client's browser must support form-based upload. Something like:
<FORM ENCTYPE="multipart/form-data"
method="POST" action="/theservlet">
<INPUT TYPE="file" NAME="me.jpg">
<INPUT TYPE="submit" VALUE="upload">
</FORM>
Then in order to code a servlet being able of processing the POST data to extract the encoded file you can take a look to the required format in the RFC-1867. ( http://www.ietf.org/rfc/rfc1867.txt ).
If you prefer to use a library to do it instead of code your own servlet, here you have a number of libraries available:
.-Jason Hunter's MultipartRequest at http://www.servlets.com
.-CParseRFC1867 (available from http://www.servletcentral.com).
.-There are MIME-parsing routines in JavaMail
.-www.JSPSmart.com has a free set of JSP for uploading and downloading files.

-HTH
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Shubhrajiit (and all the others),
thx for the info. I have the problem, that I cannont deploy any Java packages to the production server (that is a political, not a technical restriction). Could I extract the class(es) needed from the oreilly package? What classes would that be?
Originally posted by Shubhrajit Chatterjee:
for example you can look at the following package ...
com.oreilly.servlet
Malhar Barai
Author
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
I have the problem, that I cannont deploy any Java packages to the production server (that is a political, not a technical restriction). Could I extract the class(es) needed from the oreilly package? What classes would that be?

dear stephan
i think u must have dwnloaded the com.oreilly... package. u must be having a directory structure like com->oreilly->servlet...now on the server u can make a similar directory structure inside the servlet directory...
hope this helps...it did for me...still if that doesnt..just revert...
malhar
(A doesn't cost anything...keep smiling)
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Stephan,

You've got an interesting problem. By that I mean: If you can't deploy any java packages on your production server, how do the Mordacs in your organization expect you to deploy your own application? (Mordac: Preventer of Information Services)

Anyways... If you're able to deploy WAR files (your application) then you can utilize the WEB-INF/lib directory within this package to 'hide' the cos.jar file. It would stil be a 'java package', but it would be 'part' of your application. You can make the political case that it won't work without it.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi there,
thx for the help. I need to elaborate a little. The server I'm deploying to is a Lotus Domino R5. It has only a JRE 1.1.8 and no real deployment mechanism for all the nice J2EE stuff. So I can put small servlets in the servlet directory, that's it more or less.
So how would I go with the com.oreilly.servlet. I asume the book has the source-code. I probably could migrate the one function I need (and it's depencencies) into my source file. However I'm a VERY GREEN Java Horn , so how to go?
If you think education is expensive - try ignorance!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sample for a servlet handling file upload?
 
Similar Threads
upload limit
Is there any other alternate to find file size and restrict it?
FileUpload and File type
Browse/Upload Button
File upload filter for displaying files