• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:


how to upload an image file to the server using servlets ?
  • 2
1 Solution
20. How do I upload a file to my servlet?
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 (ak@hplb.hpl.hp.com) at http://www-
uk.hpl.hp.com/people/ak/java/#utils. JavaMail also has MIME-parsing routines (see the References section).

Here is an example of HTML code that allows file upload, courtesy of Detlef Pleiss (dpleiss@os-net.de) :

<FORM ENCTYPE="multipart/form-data" method=post
action="..."> put the servlet URL here, of course
<INPUT TYPE="file" NAME="mptest"><INPUT TYPE="submit" VALUE="upload">

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.
Paul wrote:
Go to Oreilly's web site(http://www.oreilly.com), navigate to Java section, finally get to the book titled [Java servlet
programming]. You can download its sample code in a zip file. In chapter 6, you will find the upload.html and
unloadtest.java. But you need to download an extra utility class stored in a zip file named
[cos.zip]http://www.servlets.com/resources/com.oreilly.servlet/cos.zip. The URL is
With these files, you wil be able to send any type of files.

With those example files, you can send multiple files by adding more input fields in the html file. You need to
modify the servlet if you want to see the whole list of the files you send instead of the last one. You just need to use
an array to keep the list of files you receive. There is no limit to the size of files you can send. The Sun's examples
do have size limitation.


  If you are Using your Own Servlets then it's simple to upload a file from your applet too ,
  check out this

  Also go to
  and look for class MultipartRequest's code

  http://www.sun.com/software/jwebserver/faq/faq.html#c8  and

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now