Skip to content

Commit eceb738

Browse files
committed
BAEL-1740
1 parent 506962b commit eceb738

7 files changed

Lines changed: 183 additions & 45 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.servlets;
2+
3+
public class Constants {
4+
5+
public static final String UPLOAD_DIRECTORY = "upload";
6+
public static final String ENDPOINT = "/jspupload/uploadFile";
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+
12+
}

‎javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
2424
response.setHeader("Test", "Success");
2525
response.setHeader("BMI", String.valueOf(bmi));
2626

27-
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp");
27+
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/upload.jsp");
2828
dispatcher.forward(request, response);
2929
} catch (Exception e) {
30-
response.sendRedirect("index.jsp");
30+
response.sendRedirect("upload.jsp");
3131
}
3232
}
3333

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.servlets;
2+
3+
import static com.baeldung.servlets.Constants.MAX_FILE_SIZE;
4+
import static com.baeldung.servlets.Constants.MAX_REQUEST_SIZE;
5+
import static com.baeldung.servlets.Constants.MEMORY_THRESHOLD;
6+
import static com.baeldung.servlets.Constants.UPLOAD_DIRECTORY;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.util.List;
11+
import javax.servlet.ServletException;
12+
import javax.servlet.http.HttpServlet;
13+
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletResponse;
15+
import org.apache.commons.fileupload.FileItem;
16+
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
17+
import org.apache.commons.fileupload.servlet.ServletFileUpload;
18+
19+
public class UploadServlet extends HttpServlet {
20+
21+
private static final long serialVersionUID = 1L;
22+
23+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
24+
throws ServletException, IOException {
25+
26+
if (ServletFileUpload.isMultipartContent(request)) {
27+
28+
DiskFileItemFactory factory = new DiskFileItemFactory();
29+
factory.setSizeThreshold(MEMORY_THRESHOLD);
30+
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
31+
32+
ServletFileUpload upload = new ServletFileUpload(factory);
33+
upload.setFileSizeMax(MAX_FILE_SIZE);
34+
upload.setSizeMax(MAX_REQUEST_SIZE);
35+
String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;
36+
File uploadDir = new File(uploadPath);
37+
if (!uploadDir.exists()) {
38+
uploadDir.mkdir();
39+
}
40+
41+
try {
42+
List<FileItem> formItems = upload.parseRequest(request);
43+
44+
if (formItems != null && formItems.size() > 0) {
45+
for (FileItem item : formItems) {
46+
if (!item.isFormField()) {
47+
String fileName = new File(item.getName()).getName();
48+
String filePath = uploadPath + File.separator + fileName;
49+
File storeFile = new File(filePath);
50+
item.write(storeFile);
51+
request.setAttribute("message", "File " + fileName + " has uploaded successfully!");
52+
}
53+
}
54+
}
55+
} catch (Exception ex) {
56+
request.setAttribute("message","There was an error: " + ex.getMessage());
57+
}
58+
getServletContext().getRequestDispatcher("/result.jsp").forward(request, response);
59+
}
60+
}
61+
}
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5-
version="3.1">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5+
version="3.1">
66

7+
<servlet>
8+
<servlet-name>UploadServlet</servlet-name>
9+
<servlet-class>com.baeldung.servlets.UploadServlet</servlet-class>
10+
</servlet>
11+
<servlet-mapping>
12+
<servlet-name>UploadServlet</servlet-name>
13+
<url-pattern>/uploadFile</url-pattern>
14+
</servlet-mapping>
715
</web-app>
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
<form method="post" action="uploadFile" enctype="multipart/form-data">
12+
Choose a file: <input type="file" name="uploadFile" /><input type="submit" value="Upload" />
13+
</form>
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)