How do I upload a file to a servlet?
Author: Deron Eriksson
Description: This tutorial describes how to upload a file to a servlet.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0) || Tomcat 5.5.20

Page:    1 2 >

The ApacheSW Commons FileUploadS library makes it very easy to upload files to a servletW. Commons FileUpload can be downloaded at Commons FileUpload depends on Commons IOS. Commons IO can be downloaded at

This tutorial will utilize a project with the structure shown below. I added commons-fileupload-1.2.jar and commons-io-1.3.2.jar to the project's build path.

'file-upload' project

I associated the fileupload source and javadoc jars with the regular fileupload jarW file for convenience.

source and javadoc jars associated with regular jar (for fun)

The upload.jsp file contains a "multipart/form-data" form for uploading 3 files to the "test" servlet, indicated by the action attribute. The file fields are named file1, file2, and file3. The form also submits a hidden field named hiddenfield1 with "ok" as its value.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Page</title>
	<form name="form1" id="form1" action="test" method="post" enctype="multipart/form-data">
	<input type="hidden" name="hiddenfield1" value="ok">
	Files to upload:
	<input type="file" size="50" name="file1">
	<input type="file" size="50" name="file2">
	<input type="file" size="50" name="file3">
	<input type="submit" value="Upload">

The web.xmlW file contains a mapping of "/test" to our TestServlet.


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="file-upload" version="2.4" xmlns="" xmlns:xsi=""

The TestServlet class is the servlet responsible for receiving the form data (ie, the uploaded files).

package test;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class TestServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		doPost(request, response);

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();


		boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
		if (!isMultipartContent) {
			out.println("You are not trying to upload<br/>");
		out.println("You are trying to upload<br/>");

		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		try {
			List<FileItem> fields = upload.parseRequest(request);
			out.println("Number of fields: " + fields.size() + "<br/><br/>");
			Iterator<FileItem> it = fields.iterator();
			if (!it.hasNext()) {
				out.println("No fields found");
			out.println("<table border=\"1\">");
			while (it.hasNext()) {
				FileItem fileItem =;
				boolean isFormField = fileItem.isFormField();
				if (isFormField) {
					out.println("<td>regular form field</td><td>FIELD NAME: " + fileItem.getFieldName() + 
							"<br/>STRING: " + fileItem.getString()
				} else {
					out.println("<td>file form field</td><td>FIELD NAME: " + fileItem.getFieldName() +
							"<br/>STRING: " + fileItem.getString() +
							"<br/>NAME: " + fileItem.getName() +
							"<br/>CONTENT TYPE: " + fileItem.getContentType() +
							"<br/>SIZE (BYTES): " + fileItem.getSize() +
							"<br/>TO STRING: " + fileItem.toString()
		} catch (FileUploadException e) {

It's easy to determine if multipart data is present via the call to ServletFileUpload.isMultipartContent(request). The servlet creates a DiskFileItemFactory object and a ServletFileUpload object with the FileItemFactory interface to the DiskFileItemFactory passed in its constructor. A list of "FileItems" (ie, fields) is obtained from the ServletFileUpload object's parseRequest() method. The servlet iterates over the list of FileItems and displays information about each field.

(Continued on page 2)

Page:    1 2 >