Press enter to see results or esc to cancel.


JAVA JSP file upload example source code


In this video I have shown how you can create a JSP file upload. This project is shown in Eclipse IDE.
1. Project Structure


2. The jar file used in the project are,
1.org.apache.commons.io.jar
2.commons-fileupload-1.3.1.jar

3. The servlet will upload the image to d:\uploaded_files folder

FileUploadHandler.java


package com.chilyfacts.com;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUploadHandler extends HttpServlet {
 private static final long serialVersionUID = 1 ;
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
 doPost(request, response);
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
 String file_name = null;
 response.setContentType("text/html");
 PrintWriter out = response.getWriter();
 boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
 if (!isMultipartContent) {
 return;
 }
 FileItemFactory factory = new DiskFileItemFactory();
 ServletFileUpload upload = new ServletFileUpload(factory);
 try {
 List < FileItem > fields = upload.parseRequest(request);
 Iterator < FileItem > it = fields.iterator();
 if (!it.hasNext()) {
 return;
 }
 while (it.hasNext()) {
 FileItem fileItem = it.next();
 boolean isFormField = fileItem.isFormField();
 if (isFormField) {
 if (file_name == null) {
 if (fileItem.getFieldName().equals("file_name")) {
 file_name = fileItem.getString();
 }
 }
 } else {
 if (fileItem.getSize() > 0) {
 fileItem.write(new File("D:\\uploaded_files\\" + fileItem.getName()));
 }
 }
 }
 } catch (Exception e) {
 e.printStackTrace();
 } finally {
 out.println("<script type='text/javascript'>");
 out.println("window.location.href='index.jsp?filename="+file_name+"'");
 out.println("</script>");
 out.close();
 }
 }
}

4.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 <display-name>JSP_File_Upload</display-name>
 <welcome-file-list>
 <welcome-file>index.html</welcome-file>
 <welcome-file>index.htm</welcome-file>
 <welcome-file>index.jsp</welcome-file>
 <welcome-file>default.html</welcome-file>
 <welcome-file>default.htm</welcome-file>
 <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
 <servlet-name>FileUploadHandler</servlet-name>
 <servlet-class>com.chilyfacts.com.FileUploadHandler</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>FileUploadHandler</servlet-name>
 <url-pattern>/FileUploadHandler/*</url-pattern>
 </servlet-mapping>
 </web-app>

5. index.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>Insert title here</title>
</head>
<body>
<center>
<h1>Upload File Form</h1>
 <form action="FileUploadHandler" enctype="multipart/form-data" method="post">
 Enter File Name <input type="text" name="file_name"><br>
 Select<input type="file" name="file2" /><br>
 <input type="submit" value="upload" />
 </form> 
 <%
 String file_name=(String)request.getParameter("filename");
 if(file_name!=null){
 out.println(file_name+" File uploaded successfuly");
 }
 %>
</center>
</body>
</html>

6. After running the index.jsp in server
http://localhost:8080/JSP_File_Upload/index.jsp


7. Download complete project hereJSP_File_Upload.rar