Uploading File Using Java Servlet

Platform  : Eclipse, Apache Tomcat 7

This tutorial explains a simple Java web application to upload a file using Servlet. Although explanations in this document assume Apache Tomcat as the application server, nevertheless you are free to use another Java application server (e.g. JBoss, GlassFish, etc) by making small adaptations in the example source code.

Please follow the steps to create the application.

Getting the required libraries

This application requires two additional Java libraries to accomplish the file upload process.

  1. Commons FileUpload. The library file is available in the following site to download.


The library used in this example is version 1.3.1, but you are able to use the newer one.

  1. Commons IO. The library file is available in the following site to download.


The library used in this example is version 2.4, but you are able to use the newer one.

Please download those two libraries and extract them to get the corresponding JAR files before proceeding to the next steps.

Creating a new project

Open Eclipse and create a new project.

  1. Open the menu File –> New –> Dynamic Web Project, and name the project as servlet_fileupload.


Click Next.

  1. When arriving to this dialog, put a checkmark on the option to generate the xml.


Click Finish to complete the wizard.

  1. Using Windows Explorer, please copy the “Commons FileUpload” and “Commons IO” JAR files downloaded earlier into ..\WebContent\WEB-INF\lib under the project folder. The associated JAR files used in this example are:
  • commons-fileupload-1.3.1.jar
  • commons-io-2.4.jar

Notice that the JAR file may be different from the ones you use, depending on the version.

  1. Back to Eclipse, right click on the lib folder and select Refresh. Expand the folder and make sure the added libraries are visible.
  2. Right click on the lib folder and select Build Path –> Configure Build Path.
  3. In the Libraries tab, click the Add External JARs

Select the respective JAR files in ..\WebContent\WEB-INF\lib under the project folder.


Creating a web page to select a file

Now we are going to create a user interface to select the file to upload.

  1. In Eclipse, create a new JSP file under the WebContent folder, and name it as jsp. Open the file and modify it as follow:

The script above creates buttons to select a file from the local computer and upload to the server. Be sure not to forget the enctype=”multipart/form-data” property in the form tag.

Notice that the form has the action property to call the UploadServlet servlet. We will come to that part later on.

Modifying the web.xml

Open the web.xml under the ..\WebContent\WEB-INF folder, and add the following lines inside the <web-app> tag.

The lines above is to define the file-upload parameter specifying the target folder when a file is uploaded. It is assumed that the installation folder of the application server is C:\apache-tomcat-7.0.57, and the ..\webapps\data sub-folder is used to store the uploaded files. Please make necessary changes according to your installation of your application server.

The content of the web.xml is shown in the following picture.


Creating the servlet

As mentioned earlier, when a file is selected and form is submitted, a servlet is invoked to receive the file. Now we are going to create the associated servlet.

  1. In Eclipse, expand the project folder until the ..\Java Resources\src folder is found.
  2. Right click on the src folder and select New –> Package. Create a new package and name it as fileupload.


  1. Right click on the new package and select New –> Servlet and name it as UploadServlet.


Click Next.

  1. Be sure to ask the wizard to create the init, doPost and doGet methods as shown below.


Click Finish.

  1. Open the UploadServlet.java file and make necessary changes as follow.

 Please be aware of the following code in the doPost method to specify the temporary folder when a file is uploaded.

Make certain the folder is available in your server, or make necessary changes according to your preferred setting.

Also notice the init method above does not contain any parameter. Please remove any existing parameter if exists.

Creating the WAR file

Writing the application is done, now compile the source code and generate the installation package.

  1. Right click on the project name and select Export –> WAR File. Specify the location and file name of the WAR file.
  2. Deploy the WAR file in your application server. The detail steps are not explained here.

Running the application

Upon the application is up and running in the application server, now give it a try.

  1. Open the web application in a browser and try to upload a file.


  1. When the upload is success, the file will be available in the target folder. It is “C:\apache-tomcat-7.0.57\webapps\data” in this example (see the web.xml earlier).


Please verify the content of the files to be exactly the same with the origination.



Download source code






Leave a Reply

Your email address will not be published. Required fields are marked *