Skip to content

Commit 3c9a2f3

Browse files
authored
Merge pull request eugenp#4239 from Thoughtscript/BAEL-1740
BAEL-1740
2 parents c9f2521 + 19dd1a8 commit 3c9a2f3

8 files changed

Lines changed: 251 additions & 47 deletions

File tree

javax-servlets/pom.xml

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,75 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
<artifactId>javax-servlets</artifactId>
6-
<version>1.0-SNAPSHOT</version>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>javax-servlets</artifactId>
7+
<version>1.0-SNAPSHOT</version>
78

8-
<parent>
9-
<groupId>com.baeldung</groupId>
10-
<artifactId>parent-modules</artifactId>
11-
<version>1.0.0-SNAPSHOT</version>
12-
</parent>
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>parent-modules</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
</parent>
1314

14-
<dependencies>
15-
<dependency>
16-
<groupId>javax.servlet</groupId>
17-
<artifactId>javax.servlet-api</artifactId>
18-
<version>${javax.servlet.version}</version>
19-
</dependency>
20-
<dependency>
21-
<groupId>org.apache.httpcomponents</groupId>
22-
<artifactId>httpclient</artifactId>
23-
<version>${org.apache.httpcomponents.version}</version>
24-
<scope>test</scope>
25-
<exclusions>
26-
<exclusion>
27-
<artifactId>commons-logging</artifactId>
28-
<groupId>commons-logging</groupId>
29-
</exclusion>
30-
</exclusions>
31-
</dependency>
32-
<dependency>
33-
<groupId>org.springframework</groupId>
34-
<artifactId>spring-test</artifactId>
35-
<version>${spring-test.version}</version>
36-
<scope>test</scope>
37-
</dependency>
38-
</dependencies>
15+
<dependencies>
16+
<!-- File Uploading -->
17+
<dependency>
18+
<groupId>commons-fileupload</groupId>
19+
<artifactId>commons-fileupload</artifactId>
20+
<version>1.3.3</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>commons-io</groupId>
24+
<artifactId>commons-io</artifactId>
25+
<version>2.6</version>
26+
</dependency>
3927

40-
<properties>
41-
<javax.servlet.version>3.1.0</javax.servlet.version>
42-
<org.apache.httpcomponents.version>4.5.3</org.apache.httpcomponents.version>
43-
<spring-test.version>5.0.5.RELEASE</spring-test.version>
44-
</properties>
28+
<!-- Servlet -->
29+
<dependency>
30+
<groupId>javax.servlet</groupId>
31+
<artifactId>javax.servlet-api</artifactId>
32+
<version>4.0.1</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>javax.servlet.jsp.jstl</groupId>
36+
<artifactId>jstl-api</artifactId>
37+
<version>1.2</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>javax.servlet.jsp</groupId>
41+
<artifactId>javax.servlet.jsp-api</artifactId>
42+
<version>2.3.1</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>javax.servlet</groupId>
46+
<artifactId>jstl</artifactId>
47+
<version>1.2</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.apache.httpcomponents</groupId>
52+
<artifactId>httpclient</artifactId>
53+
<version>${org.apache.httpcomponents.version}</version>
54+
<scope>test</scope>
55+
<exclusions>
56+
<exclusion>
57+
<artifactId>commons-logging</artifactId>
58+
<groupId>commons-logging</groupId>
59+
</exclusion>
60+
</exclusions>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.springframework</groupId>
64+
<artifactId>spring-test</artifactId>
65+
<version>${spring-test.version}</version>
66+
<scope>test</scope>
67+
</dependency>
68+
</dependencies>
69+
70+
<properties>
71+
<org.apache.httpcomponents.version>4.5.3</org.apache.httpcomponents.version>
72+
<spring-test.version>5.0.5.RELEASE</spring-test.version>
73+
</properties>
4574

4675
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung;
2+
3+
public class Constants {
4+
5+
public static final String UPLOAD_DIRECTORY = "upload";
6+
public static final String DEFAULT_FILENAME = "default.file";
7+
8+
public static final int MEMORY_THRESHOLD = 1024 * 1024 * 3;
9+
public static final int MAX_FILE_SIZE = 1024 * 1024 * 40;
10+
public static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50;
11+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.servlets;
2+
3+
import com.baeldung.Constants;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.annotation.MultipartConfig;
7+
import javax.servlet.annotation.WebServlet;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
import javax.servlet.http.Part;
12+
import java.io.File;
13+
import java.io.FileNotFoundException;
14+
import java.io.IOException;
15+
16+
import static com.baeldung.Constants.UPLOAD_DIRECTORY;
17+
18+
@WebServlet(
19+
name = "MultiPartServlet",
20+
urlPatterns = {"/multiPartServlet"}
21+
)
22+
@MultipartConfig(fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024 * 5, maxRequestSize = 1024 * 1024 * 5 * 5)
23+
public class MultipartServlet extends HttpServlet {
24+
25+
private static final long serialVersionUID = 1L;
26+
27+
private String getFileName(Part part) {
28+
for (String content : part.getHeader("content-disposition").split(";")) {
29+
if (content.trim().startsWith("filename"))
30+
return content.substring(content.indexOf("=") + 2, content.length() - 1);
31+
}
32+
return Constants.DEFAULT_FILENAME;
33+
}
34+
35+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
36+
37+
String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;
38+
File uploadDir = new File(uploadPath);
39+
if (!uploadDir.exists())
40+
uploadDir.mkdir();
41+
42+
try {
43+
String fileName = "";
44+
for (Part part : request.getParts()) {
45+
fileName = getFileName(part);
46+
part.write(uploadPath + File.separator + fileName);
47+
}
48+
request.setAttribute("message", "File " + fileName + " has uploaded successfully!");
49+
} catch (FileNotFoundException fne) {
50+
request.setAttribute("message", "There was an error: " + fne.getMessage());
51+
}
52+
getServletContext().getRequestDispatcher("/result.jsp").forward(request, response);
53+
}
54+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.servlets;
2+
3+
import javax.servlet.annotation.WebServlet;
4+
import org.apache.commons.fileupload.FileItem;
5+
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
6+
import org.apache.commons.fileupload.servlet.ServletFileUpload;
7+
8+
import javax.servlet.ServletException;
9+
import javax.servlet.http.HttpServlet;
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpServletResponse;
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.util.List;
15+
16+
import static com.baeldung.Constants.*;
17+
18+
@WebServlet(
19+
name = "UploadServlet",
20+
urlPatterns = {"/uploadFile"}
21+
)
22+
public class UploadServlet extends HttpServlet {
23+
24+
private static final long serialVersionUID = 1L;
25+
26+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
27+
28+
if (ServletFileUpload.isMultipartContent(request)) {
29+
30+
DiskFileItemFactory factory = new DiskFileItemFactory();
31+
factory.setSizeThreshold(MEMORY_THRESHOLD);
32+
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
33+
34+
ServletFileUpload upload = new ServletFileUpload(factory);
35+
upload.setFileSizeMax(MAX_FILE_SIZE);
36+
upload.setSizeMax(MAX_REQUEST_SIZE);
37+
String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;
38+
File uploadDir = new File(uploadPath);
39+
if (!uploadDir.exists()) {
40+
uploadDir.mkdir();
41+
}
42+
43+
try {
44+
List<FileItem> formItems = upload.parseRequest(request);
45+
46+
if (formItems != null && formItems.size() > 0) {
47+
for (FileItem item : formItems) {
48+
if (!item.isFormField()) {
49+
String fileName = new File(item.getName()).getName();
50+
String filePath = uploadPath + File.separator + fileName;
51+
File storeFile = new File(filePath);
52+
item.write(storeFile);
53+
request.setAttribute("message", "File " + fileName + " has uploaded successfully!");
54+
}
55+
}
56+
}
57+
} catch (Exception ex) {
58+
request.setAttribute("message", "There was an error: " + ex.getMessage());
59+
}
60+
getServletContext().getRequestDispatcher("/result.jsp").forward(request, response);
61+
}
62+
}
63+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.servlets;
2+
3+
import java.io.IOException;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.annotation.WebServlet;
6+
import javax.servlet.http.HttpServlet;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
@WebServlet(name = "UploadWelcomeServlet", urlPatterns = "/uploadwelcome")
11+
public class UploadWelcomeServlet extends HttpServlet {
12+
13+
@Override
14+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
15+
throws ServletException, IOException {
16+
request.getRequestDispatcher("/upload.jsp").forward(request, response);
17+
}
18+
}

javax-servlets/src/main/webapp/WEB-INF/web.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3+
<html>
4+
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>Upload Result</title>
8+
</head>
9+
10+
<body>
11+
<h2>${message}</h2>
12+
</body>
13+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3+
<html>
4+
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>File Upload Demo</title>
8+
</head>
9+
10+
<body>
11+
<div>Apache FileUpload</div>
12+
<form method="post" action="uploadFile" enctype="multipart/form-data">
13+
Choose a file: <input type="file" name="uploadFile"/><input type="submit" value="Upload"/>
14+
</form>
15+
16+
</br>
17+
18+
<div>Servlet Multipart</div>
19+
<form method="post" action="multiPartServlet" enctype="multipart/form-data">
20+
Choose a file: <input type="file" name="multiPartServlet"/><input type="submit" value="Upload"/>
21+
</form>
22+
</body>
23+
</html>

0 commit comments

Comments
 (0)