commit
3c9a2f3215
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>javax-servlets</artifactId>
|
||||
|
@ -12,11 +13,40 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- File Uploading -->
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
<version>1.3.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Servlet -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet.version}</version>
|
||||
<version>4.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp.jstl</groupId>
|
||||
<artifactId>jstl-api</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp</groupId>
|
||||
<artifactId>javax.servlet.jsp-api</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
|
@ -38,7 +68,6 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<javax.servlet.version>3.1.0</javax.servlet.version>
|
||||
<org.apache.httpcomponents.version>4.5.3</org.apache.httpcomponents.version>
|
||||
<spring-test.version>5.0.5.RELEASE</spring-test.version>
|
||||
</properties>
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung;
|
||||
|
||||
public class Constants {
|
||||
|
||||
public static final String UPLOAD_DIRECTORY = "upload";
|
||||
public static final String DEFAULT_FILENAME = "default.file";
|
||||
|
||||
public static final int MEMORY_THRESHOLD = 1024 * 1024 * 3;
|
||||
public static final int MAX_FILE_SIZE = 1024 * 1024 * 40;
|
||||
public static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50;
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.servlets;
|
||||
|
||||
import com.baeldung.Constants;
|
||||
|
||||
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;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
import static com.baeldung.Constants.UPLOAD_DIRECTORY;
|
||||
|
||||
@WebServlet(
|
||||
name = "MultiPartServlet",
|
||||
urlPatterns = {"/multiPartServlet"}
|
||||
)
|
||||
@MultipartConfig(fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024 * 5, maxRequestSize = 1024 * 1024 * 5 * 5)
|
||||
public class MultipartServlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String getFileName(Part part) {
|
||||
for (String content : part.getHeader("content-disposition").split(";")) {
|
||||
if (content.trim().startsWith("filename"))
|
||||
return content.substring(content.indexOf("=") + 2, content.length() - 1);
|
||||
}
|
||||
return Constants.DEFAULT_FILENAME;
|
||||
}
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;
|
||||
File uploadDir = new File(uploadPath);
|
||||
if (!uploadDir.exists())
|
||||
uploadDir.mkdir();
|
||||
|
||||
try {
|
||||
String fileName = "";
|
||||
for (Part part : request.getParts()) {
|
||||
fileName = getFileName(part);
|
||||
part.write(uploadPath + File.separator + fileName);
|
||||
}
|
||||
request.setAttribute("message", "File " + fileName + " has uploaded successfully!");
|
||||
} catch (FileNotFoundException fne) {
|
||||
request.setAttribute("message", "There was an error: " + fne.getMessage());
|
||||
}
|
||||
getServletContext().getRequestDispatcher("/result.jsp").forward(request, response);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.servlets;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static com.baeldung.Constants.*;
|
||||
|
||||
@WebServlet(
|
||||
name = "UploadServlet",
|
||||
urlPatterns = {"/uploadFile"}
|
||||
)
|
||||
public class UploadServlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
if (ServletFileUpload.isMultipartContent(request)) {
|
||||
|
||||
DiskFileItemFactory factory = new DiskFileItemFactory();
|
||||
factory.setSizeThreshold(MEMORY_THRESHOLD);
|
||||
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
|
||||
|
||||
ServletFileUpload upload = new ServletFileUpload(factory);
|
||||
upload.setFileSizeMax(MAX_FILE_SIZE);
|
||||
upload.setSizeMax(MAX_REQUEST_SIZE);
|
||||
String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;
|
||||
File uploadDir = new File(uploadPath);
|
||||
if (!uploadDir.exists()) {
|
||||
uploadDir.mkdir();
|
||||
}
|
||||
|
||||
try {
|
||||
List<FileItem> formItems = upload.parseRequest(request);
|
||||
|
||||
if (formItems != null && formItems.size() > 0) {
|
||||
for (FileItem item : formItems) {
|
||||
if (!item.isFormField()) {
|
||||
String fileName = new File(item.getName()).getName();
|
||||
String filePath = uploadPath + File.separator + fileName;
|
||||
File storeFile = new File(filePath);
|
||||
item.write(storeFile);
|
||||
request.setAttribute("message", "File " + fileName + " has uploaded successfully!");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
request.setAttribute("message", "There was an error: " + ex.getMessage());
|
||||
}
|
||||
getServletContext().getRequestDispatcher("/result.jsp").forward(request, response);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.servlets;
|
||||
|
||||
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;
|
||||
|
||||
@WebServlet(name = "UploadWelcomeServlet", urlPatterns = "/uploadwelcome")
|
||||
public class UploadWelcomeServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
request.getRequestDispatcher("/upload.jsp").forward(request, response);
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
version="3.1">
|
||||
|
||||
</web-app>
|
|
@ -0,0 +1,13 @@
|
|||
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
||||
<!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=UTF-8">
|
||||
<title>Upload Result</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h2>${message}</h2>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,23 @@
|
|||
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
|
||||
<!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=UTF-8">
|
||||
<title>File Upload Demo</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div>Apache FileUpload</div>
|
||||
<form method="post" action="uploadFile" enctype="multipart/form-data">
|
||||
Choose a file: <input type="file" name="uploadFile"/><input type="submit" value="Upload"/>
|
||||
</form>
|
||||
|
||||
</br>
|
||||
|
||||
<div>Servlet Multipart</div>
|
||||
<form method="post" action="multiPartServlet" enctype="multipart/form-data">
|
||||
Choose a file: <input type="file" name="multiPartServlet"/><input type="submit" value="Upload"/>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue