diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a15b58d --- /dev/null +++ b/pom.xml @@ -0,0 +1,77 @@ + + + + 4.0.0 + + ru.molokoin + exchange-servlet + 0.1 + war + + exchange-servlet Maven Webapp + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + javax.servlet + javax.servlet-api + 3.1.0 + + + javax.servlet.jsp + jsp-api + 2.1 + + + junit + junit + 4.11 + test + + + + + exchange-servlet + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-war-plugin + 3.2.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + + diff --git a/src/main/java/ru/molokoin/FileDownloadServlet.java b/src/main/java/ru/molokoin/FileDownloadServlet.java new file mode 100644 index 0000000..0ef22a5 --- /dev/null +++ b/src/main/java/ru/molokoin/FileDownloadServlet.java @@ -0,0 +1,83 @@ +package ru.molokoin; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; + +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(description = "Download File From The Server", urlPatterns = { "/downloadServlet" }) +public class FileDownloadServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + public static int BUFFER_SIZE = 1024 * 100; + public static final String UPLOAD_DIR = "uploadedFiles"; + + /***** This Method Is Called By The Servlet Container To Process A 'GET' Request *****/ + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + handleRequest(request, response); + } + + public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + /***** Get The Absolute Path Of The File To Be Downloaded *****/ + String fileName = request.getParameter("fileName"), + applicationPath = getServletContext().getRealPath(""), + downloadPath = applicationPath + File.separator + UPLOAD_DIR, + filePath = downloadPath + File.separator + fileName; + + File file = new File(filePath); + OutputStream outStream = null; + FileInputStream inputStream = null; + + if (file.exists()) { + + /**** Setting The Content Attributes For The Response Object ****/ + String mimeType = "application/octet-stream"; + response.setContentType(mimeType); + + /**** Setting The Headers For The Response Object ****/ + String headerKey = "Content-Disposition"; + String headerValue = String.format("attachment; filename=\"%s\"", file.getName()); + response.setHeader(headerKey, headerValue); + + try { + + /**** Get The Output Stream Of The Response ****/ + outStream = response.getOutputStream(); + inputStream = new FileInputStream(file); + byte[] buffer = new byte[BUFFER_SIZE]; + int bytesRead = -1; + + /**** Write Each Byte Of Data Read From The Input Stream Write Each Byte Of Data Read From The Input Stream Into The Output Stream ****/ + while ((bytesRead = inputStream.read(buffer)) != -1) { + outStream.write(buffer, 0, bytesRead); + } + } catch(IOException ioExObj) { + System.out.println("Exception While Performing The I/O Operation?= " + ioExObj.getMessage()); + } finally { + if (inputStream != null) { + inputStream.close(); + } + + outStream.flush(); + if (outStream != null) { + outStream.close(); + } + } + } else { + + /***** Set Response Content Type *****/ + response.setContentType("text/html"); + + /***** Print The Response *****/ + response.getWriter().println("

File "+ fileName +" Is Not Present .....!

"); + } + } +} \ No newline at end of file diff --git a/src/main/java/ru/molokoin/FileUploadServlet.java b/src/main/java/ru/molokoin/FileUploadServlet.java new file mode 100644 index 0000000..81bb0a9 --- /dev/null +++ b/src/main/java/ru/molokoin/FileUploadServlet.java @@ -0,0 +1,75 @@ +package ru.molokoin; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.RequestDispatcher; +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; + +@WebServlet(description = "Upload File To The Server", urlPatterns = { "/fileUploadServlet" }) +@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 10, maxFileSize = 1024 * 1024 * 30, maxRequestSize = 1024 * 1024 * 50) +public class FileUploadServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + public static final String UPLOAD_DIR = "uploadedFiles"; + + /***** This Method Is Called By The Servlet Container To Process A 'POST' Request *****/ + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + handleRequest(request, response); + } + + public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + /***** Get The Absolute Path Of The Web Application *****/ + String applicationPath = getServletContext().getRealPath(""), + uploadPath = applicationPath + File.separator + UPLOAD_DIR; + + File fileUploadDirectory = new File(uploadPath); + if (!fileUploadDirectory.exists()) { + fileUploadDirectory.mkdirs(); + } + + String fileName = ""; + UploadDetail details = null; + List fileList = new ArrayList(); + + for (Part part : request.getParts()) { + fileName = extractFileName(part); + details = new UploadDetail(); + details.setFileName(fileName); + details.setFileSize(part.getSize() / 1024); + try { + part.write(uploadPath + File.separator + fileName); + details.setUploadStatus("Success"); + } catch (IOException ioObj) { + details.setUploadStatus("Failure : "+ ioObj.getMessage()); + } + fileList.add(details); + } + + request.setAttribute("uploadedFiles", fileList); + RequestDispatcher dispatcher = request.getRequestDispatcher("/fileuploadResponse.jsp"); + dispatcher.forward(request, response); + } + + /***** Helper Method #1 - This Method Is Used To Read The File Names *****/ + private String extractFileName(Part part) { + String fileName = "", + contentDisposition = part.getHeader("content-disposition"); + String[] items = contentDisposition.split(";"); + for (String item : items) { + if (item.trim().startsWith("filename")) { + fileName = item.substring(item.indexOf("=") + 2, item.length() - 1); + } + } + return fileName; + } +} \ No newline at end of file diff --git a/src/main/java/ru/molokoin/UploadDetail.java b/src/main/java/ru/molokoin/UploadDetail.java new file mode 100644 index 0000000..5ecc481 --- /dev/null +++ b/src/main/java/ru/molokoin/UploadDetail.java @@ -0,0 +1,35 @@ +package ru.molokoin; + +import java.io.Serializable; + +public class UploadDetail implements Serializable { + + private long fileSize; + private String fileName, uploadStatus; + + private static final long serialVersionUID = 1L; + + public long getFileSize() { + return fileSize; + } + + public void setFileSize(long fileSize) { + this.fileSize = fileSize; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public String getUploadStatus() { + return uploadStatus; + } + + public void setUploadStatus(String uploadStatus) { + this.uploadStatus = uploadStatus; + } +} \ No newline at end of file diff --git a/src/main/java/ru/molokoin/UploadedFilesServlet.java b/src/main/java/ru/molokoin/UploadedFilesServlet.java new file mode 100644 index 0000000..167ba5b --- /dev/null +++ b/src/main/java/ru/molokoin/UploadedFilesServlet.java @@ -0,0 +1,52 @@ +package ru.molokoin; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.RequestDispatcher; +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(description = "List The Already Uploaded Files", urlPatterns = { "/uploadedFilesServlet" }) +public class UploadedFilesServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + public static final String UPLOAD_DIR = "uploadedFiles"; + + /***** This Method Is Called By The Servlet Container To Process A 'GET' Request *****/ + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + handleRequest(request, response); + } + + public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + /***** Get The Absolute Path Of The Web Application *****/ + String applicationPath = getServletContext().getRealPath(""), + uploadPath = applicationPath + File.separator + UPLOAD_DIR; + + File fileUploadDirectory = new File(uploadPath); + if (!fileUploadDirectory.exists()) { + fileUploadDirectory.mkdirs(); + } + + UploadDetail details = null; + File[] allFiles = fileUploadDirectory.listFiles(); + List fileList = new ArrayList(); + + for (File file : allFiles) { + details = new UploadDetail(); + details.setFileName(file.getName()); + details.setFileSize(file.length() / 1024); + fileList.add(details); + } + + request.setAttribute("uploadedFiles", fileList); + RequestDispatcher dispatcher = request.getRequestDispatcher("/allfiles.jsp"); + dispatcher.forward(request, response); + } +} \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..56093df --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,10 @@ + + + + exchange-servlet + + fileupload.jsp + + diff --git a/src/main/webapp/allfiles.jsp b/src/main/webapp/allfiles.jsp new file mode 100644 index 0000000..103b375 --- /dev/null +++ b/src/main/webapp/allfiles.jsp @@ -0,0 +1,42 @@ +<%@page import="java.util.List"%> +<%@page import="ru.molokoin.UploadDetail"%> +<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> + + + + + exchange-servlet + + + + +
+

Uploaded Files

+ + + + + + <% List uploadDetails = (List)request.getAttribute("uploadedFiles"); + if(uploadDetails != null && uploadDetails.size() > 0) { + for(int i=0; i + + + + + + <% } + } else { %> + + + + <% } %> + +
File NameFile SizeAction
<%=uploadDetails.get(i).getFileName() %><%=uploadDetails.get(i).getFileSize() %> KBDownload
No Files Uploaded.....!
+
+ Back +
+
+ + \ No newline at end of file diff --git a/src/main/webapp/fileuploadResponse.jsp b/src/main/webapp/fileuploadResponse.jsp new file mode 100644 index 0000000..1d848d3 --- /dev/null +++ b/src/main/webapp/fileuploadResponse.jsp @@ -0,0 +1,37 @@ +<%@page import="java.util.List"%> +<%@page import="ru.molokoin.UploadDetail"%> +<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> + + + + + exchange-servlet + + + + +
+

File Upload Status

+ + + + + + <% List uploadDetails = (List)request.getAttribute("uploadedFiles"); + for(int i=0; i + + + + + + + <% } %> + +
File NameFile SizeUpload StatusAction
<%=uploadDetails.get(i).getFileName() %><%=uploadDetails.get(i).getFileSize() %> KB<%=uploadDetails.get(i).getUploadStatus() %>Download
+
+ Back +
+
+ + \ No newline at end of file diff --git a/src/main/webapp/resource/css/main.css b/src/main/webapp/resource/css/main.css new file mode 100644 index 0000000..2150a5e --- /dev/null +++ b/src/main/webapp/resource/css/main.css @@ -0,0 +1,73 @@ +* { + box-sizing: border-box; +} + +.margin_top_15px { + margin-top: 15px; +} + +.panel { + display: block; + padding: 15px; + border: 1px solid #c0c0c0; + width: 849px; + margin: 52px 0px 0px 32px; +} + +label { + display: inline-block; + margin-bottom: 5px; +} + +.form_group { + margin: 10px; +} + +.btn_primary { + color: #fff; + background-color: #0275d8; + border-color: #0275d8; +} + +.btn { + display: inline-block; + padding: 10px; + text-align: center; + white-space: nowrap; + vertical-align: middle; + cursor: pointer; + border: 1px solid transparent; +} + +.input_error { + color: #DE3330; + padding: 5px; +} + +.bordered_table { + border-collapse: collapse; + width: 100%; + table-layout: fixed; +} + +.bordered_table tr,th,td { + border: 1px solid #333; +} + +.bordered_table th,td { + padding: 5px; +} + +.bordered_table td { + word-wrap: break-word; +} + +#noFiles { + color: red; + font-size: larger; +} + +.hyperLink { + text-decoration: none; + cursor: pointer; +} \ No newline at end of file diff --git a/target/classes/ru/molokoin/FileDownloadServlet.class b/target/classes/ru/molokoin/FileDownloadServlet.class new file mode 100644 index 0000000..9200c9d Binary files /dev/null and b/target/classes/ru/molokoin/FileDownloadServlet.class differ diff --git a/target/classes/ru/molokoin/FileUploadServlet.class b/target/classes/ru/molokoin/FileUploadServlet.class new file mode 100644 index 0000000..5a0f9ff Binary files /dev/null and b/target/classes/ru/molokoin/FileUploadServlet.class differ diff --git a/target/classes/ru/molokoin/UploadDetail.class b/target/classes/ru/molokoin/UploadDetail.class new file mode 100644 index 0000000..532953a Binary files /dev/null and b/target/classes/ru/molokoin/UploadDetail.class differ diff --git a/target/classes/ru/molokoin/UploadedFilesServlet.class b/target/classes/ru/molokoin/UploadedFilesServlet.class new file mode 100644 index 0000000..cc372e2 Binary files /dev/null and b/target/classes/ru/molokoin/UploadedFilesServlet.class differ diff --git a/target/exchange-servlet.war b/target/exchange-servlet.war new file mode 100644 index 0000000..1a1908f Binary files /dev/null and b/target/exchange-servlet.war differ diff --git a/target/exchange-servlet/WEB-INF/classes/ru/molokoin/FileDownloadServlet.class b/target/exchange-servlet/WEB-INF/classes/ru/molokoin/FileDownloadServlet.class new file mode 100644 index 0000000..9200c9d Binary files /dev/null and b/target/exchange-servlet/WEB-INF/classes/ru/molokoin/FileDownloadServlet.class differ diff --git a/target/exchange-servlet/WEB-INF/classes/ru/molokoin/FileUploadServlet.class b/target/exchange-servlet/WEB-INF/classes/ru/molokoin/FileUploadServlet.class new file mode 100644 index 0000000..5a0f9ff Binary files /dev/null and b/target/exchange-servlet/WEB-INF/classes/ru/molokoin/FileUploadServlet.class differ diff --git a/target/exchange-servlet/WEB-INF/classes/ru/molokoin/UploadDetail.class b/target/exchange-servlet/WEB-INF/classes/ru/molokoin/UploadDetail.class new file mode 100644 index 0000000..532953a Binary files /dev/null and b/target/exchange-servlet/WEB-INF/classes/ru/molokoin/UploadDetail.class differ diff --git a/target/exchange-servlet/WEB-INF/classes/ru/molokoin/UploadedFilesServlet.class b/target/exchange-servlet/WEB-INF/classes/ru/molokoin/UploadedFilesServlet.class new file mode 100644 index 0000000..cc372e2 Binary files /dev/null and b/target/exchange-servlet/WEB-INF/classes/ru/molokoin/UploadedFilesServlet.class differ diff --git a/target/exchange-servlet/WEB-INF/lib/javax.servlet-api-3.1.0.jar b/target/exchange-servlet/WEB-INF/lib/javax.servlet-api-3.1.0.jar new file mode 100644 index 0000000..6b14c3d Binary files /dev/null and b/target/exchange-servlet/WEB-INF/lib/javax.servlet-api-3.1.0.jar differ diff --git a/target/exchange-servlet/WEB-INF/lib/jsp-api-2.1.jar b/target/exchange-servlet/WEB-INF/lib/jsp-api-2.1.jar new file mode 100644 index 0000000..c0195af Binary files /dev/null and b/target/exchange-servlet/WEB-INF/lib/jsp-api-2.1.jar differ diff --git a/target/exchange-servlet/WEB-INF/web.xml b/target/exchange-servlet/WEB-INF/web.xml new file mode 100644 index 0000000..56093df --- /dev/null +++ b/target/exchange-servlet/WEB-INF/web.xml @@ -0,0 +1,10 @@ + + + + exchange-servlet + + fileupload.jsp + + diff --git a/target/exchange-servlet/allfiles.jsp b/target/exchange-servlet/allfiles.jsp new file mode 100644 index 0000000..103b375 --- /dev/null +++ b/target/exchange-servlet/allfiles.jsp @@ -0,0 +1,42 @@ +<%@page import="java.util.List"%> +<%@page import="ru.molokoin.UploadDetail"%> +<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> + + + + + exchange-servlet + + + + +
+

Uploaded Files

+ + + + + + <% List uploadDetails = (List)request.getAttribute("uploadedFiles"); + if(uploadDetails != null && uploadDetails.size() > 0) { + for(int i=0; i + + + + + + <% } + } else { %> + + + + <% } %> + +
File NameFile SizeAction
<%=uploadDetails.get(i).getFileName() %><%=uploadDetails.get(i).getFileSize() %> KBDownload
No Files Uploaded.....!
+
+ Back +
+
+ + \ No newline at end of file diff --git a/target/exchange-servlet/fileupload.jsp b/target/exchange-servlet/fileupload.jsp new file mode 100644 index 0000000..6f13068 --- /dev/null +++ b/target/exchange-servlet/fileupload.jsp @@ -0,0 +1,29 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> + + + + + exchange-servlet + + + + + +
+

File Upload

+

Press 'CTRL' Key+Click On File To Select Multiple Files in Open Dialog

+
+
+ : + Please Upload A File! +
+ +
+
+ + + + + \ No newline at end of file diff --git a/target/exchange-servlet/fileuploadResponse.jsp b/target/exchange-servlet/fileuploadResponse.jsp new file mode 100644 index 0000000..1d848d3 --- /dev/null +++ b/target/exchange-servlet/fileuploadResponse.jsp @@ -0,0 +1,37 @@ +<%@page import="java.util.List"%> +<%@page import="ru.molokoin.UploadDetail"%> +<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> + + + + + exchange-servlet + + + + +
+

File Upload Status

+ + + + + + <% List uploadDetails = (List)request.getAttribute("uploadedFiles"); + for(int i=0; i + + + + + + + <% } %> + +
File NameFile SizeUpload StatusAction
<%=uploadDetails.get(i).getFileName() %><%=uploadDetails.get(i).getFileSize() %> KB<%=uploadDetails.get(i).getUploadStatus() %>Download
+
+ Back +
+
+ + \ No newline at end of file diff --git a/target/exchange-servlet/resource/css/main.css b/target/exchange-servlet/resource/css/main.css new file mode 100644 index 0000000..2150a5e --- /dev/null +++ b/target/exchange-servlet/resource/css/main.css @@ -0,0 +1,73 @@ +* { + box-sizing: border-box; +} + +.margin_top_15px { + margin-top: 15px; +} + +.panel { + display: block; + padding: 15px; + border: 1px solid #c0c0c0; + width: 849px; + margin: 52px 0px 0px 32px; +} + +label { + display: inline-block; + margin-bottom: 5px; +} + +.form_group { + margin: 10px; +} + +.btn_primary { + color: #fff; + background-color: #0275d8; + border-color: #0275d8; +} + +.btn { + display: inline-block; + padding: 10px; + text-align: center; + white-space: nowrap; + vertical-align: middle; + cursor: pointer; + border: 1px solid transparent; +} + +.input_error { + color: #DE3330; + padding: 5px; +} + +.bordered_table { + border-collapse: collapse; + width: 100%; + table-layout: fixed; +} + +.bordered_table tr,th,td { + border: 1px solid #333; +} + +.bordered_table th,td { + padding: 5px; +} + +.bordered_table td { + word-wrap: break-word; +} + +#noFiles { + color: red; + font-size: larger; +} + +.hyperLink { + text-decoration: none; + cursor: pointer; +} \ No newline at end of file diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties new file mode 100644 index 0000000..5be42b9 --- /dev/null +++ b/target/maven-archiver/pom.properties @@ -0,0 +1,4 @@ +#Created by Apache Maven 3.8.5 +groupId=ru.molokoin +artifactId=exchange-servlet +version=0.1 diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..db13f4a --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1,4 @@ +ru\molokoin\FileUploadServlet.class +ru\molokoin\UploadDetail.class +ru\molokoin\FileDownloadServlet.class +ru\molokoin\UploadedFilesServlet.class diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..bad9611 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,4 @@ +c:\Users\Strannik\Documents\esoe\code\exchange-servlet\src\main\java\ru\molokoin\UploadDetail.java +c:\Users\Strannik\Documents\esoe\code\exchange-servlet\src\main\java\ru\molokoin\FileUploadServlet.java +c:\Users\Strannik\Documents\esoe\code\exchange-servlet\src\main\java\ru\molokoin\UploadedFilesServlet.java +c:\Users\Strannik\Documents\esoe\code\exchange-servlet\src\main\java\ru\molokoin\FileDownloadServlet.java