jGuru home

Features Wish List About jGuruContact Us

Email     Password

MOTD: jGuru launches new design!

RegisterNotificationsCustomize
HOME
FAQS
FORUMS
ARTICLES
PEERSCOPE
LEARN
NEWS

View
FAQ Entry
Printable Entry


Search jGuru Search Help
Question How do I upload a file to my servlet?
Topics Java:API:Servlets:Files
Author Alex Chaffee
Created Sep 3, 1999 Modified Jul 24, 2000

Answer

From Thomas Moore's Servlet FAQ:

Form-based file upload requires a couple of steps.

The server must supply (and the client must support) encoding type multipart/form-data. Most current browsers do, but it's not a guarantee. Secondly (and this is usually the trickiest part), your servlet has to parse the binary data and do something with it (e.g., write it to a file on the server).

The intrepid programmer is referred to RFC 1867 for cluefulness on how to parse this data. Less brave souls can use either Jason Hunter's implementation of a MultipartRequest (available from http://www.servlets.com), or CParseRFC1867 (available from http://www.servletcentral.com).

Note that the source code is available for both of these examples, but both assume that you will be writing the file to a file on the server. Other uses (e.g. storing the file as a binary object in a database) will require adaptation.

There is a multipart/form parser availailable from Anders Kristensen (http://www-uk.hpl.hp.com/people/ak/java/, [email protected]) at http://www-uk.hpl.hp.com/people/ak/java/#utils. JavaMail also has MIME-parsing routines (see the Purple Servlet References).

Here is an example of HTML code that allows file upload, courtesy of Detlef Pleiss ([email protected]):


<FORM ENCTYPE="multipart/form-data" 
  method=post action="/myservlet"> 
<INPUT TYPE="file" NAME="mptest">
<INPUT TYPE="submit" VALUE="upload">
</FORM>
The input type "file" brings up a button for a file select box on the browser together with a text field that takes the file name once selected. The servlet uses the GET method parameters to decide what to do with the upload while the POST body of the request contains the file data to parse. Tested with IE4, IE5 and Netscape 4.5.



Comments and alternative answers

Comment on this FAQ entry

Jun Inamori has written a class called org.apache....
Alex Chaffee, May 8, 2000
Jun Inamori has written a class called org.apache.tomcat.request.ParseMime which is available in the Tomcat CVS tree.

Jason Hunter has written a class called com.oreilly.servlet.MultipartRequest available on Servlets.com



Reply to this answer/comment  Help  

Also, JSPSmart has a free set of JSP for doing file...
Alex Chaffee, Jun 5, 2000
Also, JSPSmart has a free set of JSP for doing file upload and download.

Reply to this answer/comment  Help  
Note that you can't access a file on the client system...
Alex Chaffee, Aug 14, 2000
Note that you can't access a file on the client system directly from a servlet; that would be a huge security hole. You have to ask the user for permission, and currently form-based upload is the only way to do that.

Reply to this answer/comment  Help  
I complie and run the Jason Hunter's MultipartRequest,...
Zuofeng Zeng, Nov 16, 2000
I complie and run the Jason Hunter's MultipartRequest, but it can not write local system for uploaded file. How do? [Use java.io.FileOutputStream et al.

You may need to pass in the location of the "incoming" directory as a servlet initialization parameter.

-Alex]

Reply to this answer/comment  Help  

I recently re-implemented Jason's MultipartRequest...
Geoff Soutter, Nov 29, 2000
I recently re-implemented Jason's MultipartRequest - that class is now a thin wrapper around a new class called MultipartRequest. This new class allows one to get at the data without buffering it or writing it to disk. It also has fixes to avoid performance problems associated with poorly implemented servlet containers. I donated it back to Jason in honour of the service he has provided the java/servlet community and he has uploaded it onto servlets.com; so feel free to check it out.

Reply to this answer/comment  Help  
Geoff, I am very interested in what you have done but...
Ian Davies, Dec 1, 2000
Geoff, I am very interested in what you have done but I cannot find it on servlets.com. Can you be more specific as to its whereabouts?

Thanks.

Reply to this answer/comment  Help  
I also checked servlets.com and the latest package...
Gerd Naschenweng, Dec 9, 2000
I also checked servlets.com and the latest package is dated 20.11.2000 - I could do with the classes without writing data to disk.

Reply to this answer/comment  Help  
The free class files from JSPSmart are the easiest...
Iain Delaney, Dec 15, 2000
The free class files from JSPSmart are the easiest way to go, their sample pages work fine and are simple to figure out. Why is their site written in ASP though? :)

Reply to this answer/comment  Help  
I have been using the multipart-sparser class from...
Ranjith Rufus, Mar 1, 2001  [1]
I have been using the multipart-sparser class from Oreilly.. The APi has a problem in handling files of JPG, GIF and BMP type.. But it works well with XML, HTML, txt etc..

Reply to this answer/comment  Help  
Re: I have been using the multipart-sparser class from...
Zach Gatu, Mar 21, 2001
Ranjith I want to use that Oreilly multipart-parser to read an xml document. The file will be read in from the client's disk via an HTML form and POSTed to a jsp file. How do I read the xml file? Below is a bit of my JSP code. <%! String filename = "http://localhost:8080/LibCatalog/xml/test.xml"; ... %> <% DOMParser parser = new DOMParser(); parser.parse(this.filename); ... %> As you can see, the xml file is hard coded. How do I read it from the HTML form? Do you have any code examples? Pls help. Thanks, Zach.

Reply to this answer/comment  Help  
How can I determine in which state/progress an upload...
Thorsten Giesecke, Mar 2, 2001
How can I determine in which state/progress an upload process (http client form to servlet [multipart]) is, i.e. with an applet showing a progress bar? Is there a simple way to send an applet this feedback?

[You'd have to do some hacking to get progress feedback from Jason's utility class. Once you get the feedback, sending it to an applet is straightforward (search on "applet servlet" to learn more). -Alex]

Reply to this answer/comment  Help  

I have found out source of the problem that I faced...
Ranjith Rufus, Mar 2, 2001  [1]
I have found out source of the problem that I faced in uploading files using the Jason Hunter's O'Reilly API.. The problem was more specific to the WebServer than to the API.. Apache Jserv is not capable of handling images, doc, compressed files as Mulitpart/form data.. But if you try the same API and servlet using Apache Tomcat, it does well

Reply to this answer/comment  Help  
Re: I have found out source of the problem that I faced...
Constantine Evenko, Mar 23, 2001
Yeah, you partly right. It's the problem of WebServer, but this isn't the problem of ApacheJServ, this is the problem of Apache. I try to do this, and have following results:
on Tomcat - works
on Tomcat+Apache - it isn't work (!)
on ApacheJServ+Apache - it isn't work to.

Apache says "Bad request" when in InputStream appears symbol 0xFF (255). That's why it's OK when uploading TEXT files and fails when BINARY.

There is the problem. But I still have no idea of how solve it. Did anybody have?

Reply to this answer/comment  Help  

FAQs

 

FAQ Entry

Ask  Question

Related Links
Servlets FAQ

Servlets Forum

Fundamentals of Java Servlets

Servlet-related resource list from Purple Technology

jGuru JSP FAQ

Sun Servlet Home Page

java.isavvix.com


Need help fast? Submit your Java question to 45,000 developers. Sign up now and receive $25 matching credit!























jGuru Privacy Policy   Copyright/Legal Notices