Here you will get example to upload file to server using servlet.
File uploading is a very common and important feature of any website. In servlet it can be done in many ways. But in this tutorial I am sharing the two most commonly used methods.
Also Read: Download File From Server Using Servlet Example
How to Upload File to Server Using Servlet?
Method 1: Servlet 3.0
If you are using Servlet 3.0 version then it provides option for uploading files without using any third party library.
Below example shows how to do this.
Create a dynamic web project and add following source code in respective files.
WebContent/index.html
This page gives an option for choosing a file for uploading.
<html> <head> <title>File Upload Example</title> </head> <body> <form action="uploadFile" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" value="upload"/> </form> </body> </html>
WebContent/success.html
This page is opened if uploading is successful.
<html> <head> <title>Success</title> </head> <body> <p>Uploaded Successfully!</p> </body> </html>
src/com/Upload.java
This servlet is responsible for uploading the file to server.
package com; 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("/uploadFile") @MultipartConfig(maxFileSize = 16177216) public class Upload extends HttpServlet{ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String uploadPath = "E:/data"; for(Part p : req.getParts()){ p.write(uploadPath + File.separator + getFileName(p)); } resp.sendRedirect("success.html"); } //extract file name from header private String getFileName(final Part part) { String header = part.getHeader("content-disposition"); for (String content : header.split(";")) { if (content.trim().startsWith("filename")) { return content.substring(content.indexOf('=') + 1).trim().replace("\"", ""); } } return null; } }
Method 2: Apache Commons Library
Apache commons provides some utility classes for uploading file. Download following two jars and import them in your project.
Also Read: How to Add or Import Jar in Eclipse Project
Now Create a dynamic web project and add following source code in respective files.
WebContent/index.html
<html> <head> <title>File Upload Example</title> </head> <body> <form action="uploadFile" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" value="upload"/> </form> </body> </html>
WebContent/success.html
<html> <head> <title>Success</title> </head> <body> <p>Uploaded Successfully!</p> </body> </html>
src/com/Upload.java
package com; import java.io.File; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.List; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; @WebServlet("/uploadFile") public class Upload extends HttpServlet{ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String uploadPath = "E:/data"; try{ DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List<FileItem> items = upload.parseRequest(req); for(FileItem item : items){ item.write( new File(uploadPath + File.separator + item.getName())); } } catch(Exception e){ e.printStackTrace(); } resp.sendRedirect("success.html"); } }
How to upload multiple files?
In above example I have uploaded only one file. The example will work for uploading multiple files also. You have to just add multiple attribute in file input tag on index.html.
Comment below if you have any queries or know any other method for uploading files to server using servlet.