File upload example using servlet and Jsp

Environment used:
JDK 1.8
Eclipse Luna
Web module 2.5
Tomcat 8(requires javax.servlet.3.1 jar)

Step 1:
Create an html file with file upload option
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
	<form method="post" action="upload" enctype="multipart/form-data">
		Choose a file : <input type="file" name="file"> <input
			type="submit" value="upload">

Step 2:
Create a Servlet program
1. Checks the request type
2. Get the file name, size , content type and content from FileItem object
package com.candidjava.servlet.upload;

import java.util.List;

import javax.servlet.ServletException;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

 * Servlet implementation class Uploadservlet
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private final String UPLOAD_DIRECTORY = "d:/uploads";

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (ServletFileUpload.isMultipartContent(request)) {
			try {
				String fname = null;
				String fsize = null;
				String ftype = null;
				List<FileItem> multiparts = new ServletFileUpload(
						new DiskFileItemFactory()).parseRequest(request);
				for (FileItem item : multiparts) {
					if (!item.isFormField()) {
						fname = new File(item.getName()).getName();
						fsize = new Long(item.getSize()).toString();
						ftype = item.getContentType();
						item.write(new File(UPLOAD_DIRECTORY + File.separator
								+ fname));
				// File uploaded successfully
				request.setAttribute("message", "File Uploaded Successfully");
				request.setAttribute("name", fname);
				request.setAttribute("size", fsize);
				request.setAttribute("type", ftype);
			} catch (Exception ex) {
				request.setAttribute("message", "File Upload Failed due to "
						+ ex);

		} else {
					"Sorry this Servlet only handles file upload request");

		request.getRequestDispatcher("/result.jsp").forward(request, response);

	// TODO Auto-generated method stub

Step 3:
Display the result page with file name, size and content type
<%@ page language="java" contentType="text/html; charset=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>Insert title here</title>
	<div id="result">
	File name : ${requestScope["name"]}
	<br> File size : ${requestScope["size"]}
	<br> File type : ${requestScope["type"]}


Step 4:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.1">
Output Screen shot

Download file upload example
file upload using servlet war
file upload using servlet zip

Related Post