Uploading Multiple Files Using jsp servlet | Code Factory

<%@ 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 page</title>
</head>
<body>
<form action="UploadServlet" method="post" enctype="multipart/form-data" name="form1" id="form1">
<center>
<table border="1">
<tr>
<td align="center"><b>Multiple image upload</b></td>
</tr>
<tr>
<td>
Specify file :
<input name="file" type="file" id="file" multiple>
</td>
</tr>
<tr>
<td align="center">
<input type="submit" name="Submit" value="Submit Files">
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
package com.codeFactory;import java.io.File;
import java.io.IOException;
import java.util.List;
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 org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
private final String UPLOAD_DIRECTORY = "D:/abc";
private static final long serialVersionUID = 1 L;
public UploadServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (ServletFileUpload.isMultipartContent(request))
{
try
{
List < FileItem > multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item: multiparts)
{
if (!item.isFormField())
{
String name = new File(item.getName()).getName();
item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
}
}
request.setAttribute("message", "File uploaded successfully.");
} 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);
}
}
<%@ 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>Result page</title>
</head>
<body>
<div>
<h3>${message }</h3>
</div>
</body>
</html>

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

How to Create Your Certificate and Use It With Postman

Basics of API using Postman

What is Inversion of Control