From eceb73823bbd7bf3b3b326c5308cf308fd3ce3c0 Mon Sep 17 00:00:00 2001 From: Thoughtscript Date: Sun, 13 May 2018 03:48:53 +0100 Subject: [PATCH 1/3] BAEL-1740 --- javax-servlets/pom.xml | 109 +++++++++++------- .../java/com/baeldung/servlets/Constants.java | 12 ++ .../com/baeldung/servlets/FormServlet.java | 4 +- .../com/baeldung/servlets/UploadServlet.java | 61 ++++++++++ .../src/main/webapp/WEB-INF/web.xml | 14 ++- javax-servlets/src/main/webapp/result.jsp | 13 +++ javax-servlets/src/main/webapp/upload.jsp | 15 +++ 7 files changed, 183 insertions(+), 45 deletions(-) create mode 100644 javax-servlets/src/main/java/com/baeldung/servlets/Constants.java create mode 100644 javax-servlets/src/main/java/com/baeldung/servlets/UploadServlet.java create mode 100644 javax-servlets/src/main/webapp/result.jsp create mode 100644 javax-servlets/src/main/webapp/upload.jsp diff --git a/javax-servlets/pom.xml b/javax-servlets/pom.xml index 69d952da9c..41a3bb69be 100644 --- a/javax-servlets/pom.xml +++ b/javax-servlets/pom.xml @@ -1,46 +1,75 @@ - - 4.0.0 - javax-servlets - 1.0-SNAPSHOT + + 4.0.0 + javax-servlets + 1.0-SNAPSHOT - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + - - - javax.servlet - javax.servlet-api - ${javax.servlet.version} - - - org.apache.httpcomponents - httpclient - ${org.apache.httpcomponents.version} - test - - - commons-logging - commons-logging - - - - - org.springframework - spring-test - ${spring-test.version} - test - - + + + + commons-fileupload + commons-fileupload + 1.3.3 + + + commons-io + commons-io + 2.6 + - - 3.1.0 - 4.5.3 - 5.0.5.RELEASE - + + + javax.servlet + javax.servlet-api + 4.0.1 + + + javax.servlet.jsp.jstl + jstl-api + 1.2 + + + javax.servlet.jsp + javax.servlet.jsp-api + 2.3.1 + + + javax.servlet + jstl + 1.2 + + + + org.apache.httpcomponents + httpclient + ${org.apache.httpcomponents.version} + test + + + commons-logging + commons-logging + + + + + org.springframework + spring-test + ${spring-test.version} + test + + + + + 4.5.3 + 5.0.5.RELEASE + \ No newline at end of file diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/Constants.java b/javax-servlets/src/main/java/com/baeldung/servlets/Constants.java new file mode 100644 index 0000000000..3195e95088 --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/servlets/Constants.java @@ -0,0 +1,12 @@ +package com.baeldung.servlets; + +public class Constants { + + public static final String UPLOAD_DIRECTORY = "upload"; + public static final String ENDPOINT = "/jspupload/uploadFile"; + + 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; + +} diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java index c78129a9cf..79004f9d94 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java @@ -24,10 +24,10 @@ public class FormServlet extends HttpServlet { response.setHeader("Test", "Success"); response.setHeader("BMI", String.valueOf(bmi)); - RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp"); + RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/upload.jsp"); dispatcher.forward(request, response); } catch (Exception e) { - response.sendRedirect("index.jsp"); + response.sendRedirect("upload.jsp"); } } diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/UploadServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/UploadServlet.java new file mode 100644 index 0000000000..c809a9e818 --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/servlets/UploadServlet.java @@ -0,0 +1,61 @@ +package com.baeldung.servlets; + +import static com.baeldung.servlets.Constants.MAX_FILE_SIZE; +import static com.baeldung.servlets.Constants.MAX_REQUEST_SIZE; +import static com.baeldung.servlets.Constants.MEMORY_THRESHOLD; +import static com.baeldung.servlets.Constants.UPLOAD_DIRECTORY; + +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; + +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 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); + } + } +} \ No newline at end of file diff --git a/javax-servlets/src/main/webapp/WEB-INF/web.xml b/javax-servlets/src/main/webapp/WEB-INF/web.xml index 66934d8fd3..6f1220fa12 100644 --- a/javax-servlets/src/main/webapp/WEB-INF/web.xml +++ b/javax-servlets/src/main/webapp/WEB-INF/web.xml @@ -1,7 +1,15 @@ + 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"> + + UploadServlet + com.baeldung.servlets.UploadServlet + + + UploadServlet + /uploadFile + \ No newline at end of file diff --git a/javax-servlets/src/main/webapp/result.jsp b/javax-servlets/src/main/webapp/result.jsp new file mode 100644 index 0000000000..2e985cb322 --- /dev/null +++ b/javax-servlets/src/main/webapp/result.jsp @@ -0,0 +1,13 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> + + + + + + Upload Result + + + +

${message}

+ + \ No newline at end of file diff --git a/javax-servlets/src/main/webapp/upload.jsp b/javax-servlets/src/main/webapp/upload.jsp new file mode 100644 index 0000000000..8a0fcfc220 --- /dev/null +++ b/javax-servlets/src/main/webapp/upload.jsp @@ -0,0 +1,15 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> + + + + + + File Upload Demo + + + +
+ Choose a file: +
+ + \ No newline at end of file From 368fb608f7741553ae294d021979ee41d91b425e Mon Sep 17 00:00:00 2001 From: Thoughtscript Date: Sun, 13 May 2018 23:56:37 +0100 Subject: [PATCH 2/3] BAEL-1740: Cleanup and Annotation Approach --- .../baeldung/{servlets => }/Constants.java | 7 +- .../com/baeldung/servlets/FormServlet.java | 4 +- .../baeldung/servlets/MultipartServlet.java | 49 ++++++++++ .../com/baeldung/servlets/UploadServlet.java | 91 +++++++++---------- .../src/main/webapp/WEB-INF/web.xml | 9 ++ javax-servlets/src/main/webapp/upload.jsp | 12 ++- 6 files changed, 117 insertions(+), 55 deletions(-) rename javax-servlets/src/main/java/com/baeldung/{servlets => }/Constants.java (73%) create mode 100644 javax-servlets/src/main/java/com/baeldung/servlets/MultipartServlet.java diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/Constants.java b/javax-servlets/src/main/java/com/baeldung/Constants.java similarity index 73% rename from javax-servlets/src/main/java/com/baeldung/servlets/Constants.java rename to javax-servlets/src/main/java/com/baeldung/Constants.java index 3195e95088..90c64870ad 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/Constants.java +++ b/javax-servlets/src/main/java/com/baeldung/Constants.java @@ -1,12 +1,11 @@ -package com.baeldung.servlets; +package com.baeldung; public class Constants { public static final String UPLOAD_DIRECTORY = "upload"; - public static final String ENDPOINT = "/jspupload/uploadFile"; + 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; - -} +} \ No newline at end of file diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java index 79004f9d94..c78129a9cf 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java @@ -24,10 +24,10 @@ public class FormServlet extends HttpServlet { response.setHeader("Test", "Success"); response.setHeader("BMI", String.valueOf(bmi)); - RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/upload.jsp"); + RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp"); dispatcher.forward(request, response); } catch (Exception e) { - response.sendRedirect("upload.jsp"); + response.sendRedirect("index.jsp"); } } diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/MultipartServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/MultipartServlet.java new file mode 100644 index 0000000000..18296ca1d8 --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/servlets/MultipartServlet.java @@ -0,0 +1,49 @@ +package com.baeldung.servlets; + +import com.baeldung.Constants; + +import javax.servlet.ServletException; +import javax.servlet.annotation.MultipartConfig; +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; + +@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); + } +} \ No newline at end of file diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/UploadServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/UploadServlet.java index c809a9e818..e67bc349a1 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/UploadServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/UploadServlet.java @@ -1,61 +1,58 @@ package com.baeldung.servlets; -import static com.baeldung.servlets.Constants.MAX_FILE_SIZE; -import static com.baeldung.servlets.Constants.MAX_REQUEST_SIZE; -import static com.baeldung.servlets.Constants.MEMORY_THRESHOLD; -import static com.baeldung.servlets.Constants.UPLOAD_DIRECTORY; - -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; +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.*; + public class UploadServlet extends HttpServlet { - - private static final long serialVersionUID = 1L; - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + private static final long serialVersionUID = 1L; - if (ServletFileUpload.isMultipartContent(request)) { + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - DiskFileItemFactory factory = new DiskFileItemFactory(); - factory.setSizeThreshold(MEMORY_THRESHOLD); - factory.setRepository(new File(System.getProperty("java.io.tmpdir"))); + if (ServletFileUpload.isMultipartContent(request)) { - 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(); - } + DiskFileItemFactory factory = new DiskFileItemFactory(); + factory.setSizeThreshold(MEMORY_THRESHOLD); + factory.setRepository(new File(System.getProperty("java.io.tmpdir"))); - try { - List formItems = upload.parseRequest(request); + 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(); + } - 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); - } - } + try { + List 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); + } + } } \ No newline at end of file diff --git a/javax-servlets/src/main/webapp/WEB-INF/web.xml b/javax-servlets/src/main/webapp/WEB-INF/web.xml index 6f1220fa12..e5a854f5d0 100644 --- a/javax-servlets/src/main/webapp/WEB-INF/web.xml +++ b/javax-servlets/src/main/webapp/WEB-INF/web.xml @@ -12,4 +12,13 @@ UploadServlet /uploadFile + + + MultiPartServlet + com.baeldung.servlets.MultipartServlet + + + MultiPartServlet + /multiPartServlet + \ No newline at end of file diff --git a/javax-servlets/src/main/webapp/upload.jsp b/javax-servlets/src/main/webapp/upload.jsp index 8a0fcfc220..f365484892 100644 --- a/javax-servlets/src/main/webapp/upload.jsp +++ b/javax-servlets/src/main/webapp/upload.jsp @@ -1,4 +1,4 @@ -<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> @@ -8,8 +8,16 @@ +
Apache FileUpload
- Choose a file: + Choose a file: +
+ +
+ +
Servlet Multipart
+
+ Choose a file:
\ No newline at end of file From 19dd1a83ae39fb986e73144604ff69fdda2ab188 Mon Sep 17 00:00:00 2001 From: Thoughtscript Date: Mon, 14 May 2018 10:51:17 +0100 Subject: [PATCH 3/3] Servlet for upload.jsp --- .../baeldung/servlets/MultipartServlet.java | 5 ++++ .../com/baeldung/servlets/UploadServlet.java | 5 ++++ .../servlets/UploadWelcomeServlet.java | 18 ++++++++++++++ .../src/main/webapp/WEB-INF/web.xml | 24 ------------------- 4 files changed, 28 insertions(+), 24 deletions(-) create mode 100644 javax-servlets/src/main/java/com/baeldung/servlets/UploadWelcomeServlet.java delete mode 100644 javax-servlets/src/main/webapp/WEB-INF/web.xml diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/MultipartServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/MultipartServlet.java index 18296ca1d8..185edcf916 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/MultipartServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/MultipartServlet.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,10 @@ 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 { diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/UploadServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/UploadServlet.java index e67bc349a1..4de7b20913 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/UploadServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/UploadServlet.java @@ -1,5 +1,6 @@ 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; @@ -14,6 +15,10 @@ 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; diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/UploadWelcomeServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/UploadWelcomeServlet.java new file mode 100644 index 0000000000..56cec546be --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/servlets/UploadWelcomeServlet.java @@ -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); + } +} diff --git a/javax-servlets/src/main/webapp/WEB-INF/web.xml b/javax-servlets/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index e5a854f5d0..0000000000 --- a/javax-servlets/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - UploadServlet - com.baeldung.servlets.UploadServlet - - - UploadServlet - /uploadFile - - - - MultiPartServlet - com.baeldung.servlets.MultipartServlet - - - MultiPartServlet - /multiPartServlet - - \ No newline at end of file