All sizes are measured in bytes.
for (Part part : request.getParts()) {
String fileName = extractFileName(part);
part.write(fileName);
}The above code simply iterates over all parts of the request, and save each part to disk using the write() method. The file name is extracted in the following method: private String extractFileName(Part part) {
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items) {
if (s.trim().startsWith("filename")) {
return s.substring(s.indexOf("=") + 2, s.length()-1);
}
}
return "";
}Because file name of the upload file is included in content-disposition header like this:form-data; name="dataFile"; filename="PHOTO.JPG"
So the extractFileName() method gets PHOTO.JPG out of the string.package net.codejava.servlet;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet("/UploadServlet")
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10, // 10MB
maxRequestSize=1024*1024*50) // 50MB
public class UploadServlet extends HttpServlet {
/**
* Name of the directory where uploaded files will be saved, relative to
* the web application directory.
*/
private static final String SAVE_DIR = "uploadFiles";
/**
* handles file upload
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// gets absolute path of the web application
String appPath = request.getServletContext().getRealPath("");
// constructs path of the directory to save uploaded file
String savePath = appPath + File.separator + SAVE_DIR;
// creates the save directory if it does not exists
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
for (Part part : request.getParts()) {
String fileName = extractFileName(part);
// refines the fileName in case it is an absolute path
fileName = new File(fileName).getName();
part.write(savePath + File.separator + fileName);
}
request.setAttribute("message", "Upload has been done successfully!");
getServletContext().getRequestDispatcher("/message.jsp").forward(
request, response);
}
/**
* Extracts file name from HTTP header content-disposition
*/
private String extractFileName(Part part) {
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items) {
if (s.trim().startsWith("filename")) {
return s.substring(s.indexOf("=") + 2, s.length()-1);
}
}
return "";
}
}Note that in the servlet’s doPost() method, we save the uploaded file in a directory named “uploadFiles” which is relative to the web application directory. If this directory does not exist, it will be created. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>File Upload</title> </head> <body> <center> <h1>File Upload</h1> <form method="post" action="UploadServlet" enctype="multipart/form-data"> Select file to upload: <input type="file" name="file" size="60" /><br /> <br /> <input type="submit" value="Upload" /> </form> </center> </body> </html>
Following is source code the result page (message.jsp):
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload</title>
</head>
<body>
<h2>${requestScope.message}</h2>
</body>
</html>NOTE: There is no web.xml configuration file because from Servlet 3.0, web.xml becomes optional. Deploy the UploadServlet30.war file on a Tomcat version that supports Servlet 3.0 API (e.g. Tomcat 7.0). Type the following URL into browser’s address bar:
http://localhost:8080/UploadServlet30/upload.jsp
The upload form appears:

Click on Choose File button (Chrome) or Browse (FireFox/IE) to pick up a file, and hit Upload. After the file is uploaded to the server, a successful message appears:

The file is stored under:
$TOMCAT_HOME\webapps\UploadServlet30\uploadFiles
You can download an Eclipse project or a deployable WAR file in the attachment section.In addition, you can watch the following video tutorial to see how to use, deploy and test the project in action:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.