File upload example using servlet and Jsp
By candid | Posted :
Aug 22, 2015
| Updated :
Aug 22, 2015
Environment used:
JDK 1.8
Eclipse Luna
Web module 2.5
Tomcat 8(requires javax.servlet.3.1 jar)
commons-io-2.4.jar
commons-fileupload-1.2.2.jar
Step 1:
Create an html file with file upload option
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="upload" enctype="multipart/form-data">
Choose a file : <input type="file" name="file"> <input
type="submit" value="upload">
</form>
</body>
</html>
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.io.File;
import java.io.IOException;
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 {
request.setAttribute("message",
"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"
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>
<div id="result">
<h3>${requestScope["message"]}</h3>
<br>
</div>
File name : ${requestScope["name"]}
<br> File size : ${requestScope["size"]}
<br> File type : ${requestScope["type"]}
</body>
</html>
Step 4:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<welcome-file-list>
<welcome-file>fileupload.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Uploadservlet</servlet-name>
<servlet-class>com.candidjava.servlet.upload.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Uploadservlet</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
</web-app>
Output Screen shot
Download file upload example
Related Post
Comments