From fbcd9e913f8ac11f3a4cd503008e7d7d11fc754d Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 3 May 2019 08:44:52 +0200 Subject: [PATCH] + Anfang Dateien Verwaltung Signed-off-by: Johannes Theiner #SPM-7 add work development 1h --- pom.xml | 13 +++++ .../java/de/hsel/spm/baudas/web/Data.java | 39 +++++++++++++++ .../java/de/hsel/spm/baudas/web/Files.java | 29 +++++++++++ .../java/de/hsel/spm/baudas/web/Upload.java | 49 +++++++++++++++++++ src/main/webapp/version.jsp | 7 +++ 5 files changed, 137 insertions(+) create mode 100644 src/main/java/de/hsel/spm/baudas/web/Data.java create mode 100644 src/main/java/de/hsel/spm/baudas/web/Files.java create mode 100644 src/main/java/de/hsel/spm/baudas/web/Upload.java create mode 100644 src/main/webapp/version.jsp diff --git a/pom.xml b/pom.xml index 1c9e45e..b7c613d 100644 --- a/pom.xml +++ b/pom.xml @@ -80,6 +80,13 @@ TomcatServer /bauDas + + + javax.servlet + javax.servlet-api + 4.0.1 + + org.apache.maven.plugins @@ -258,6 +265,12 @@ 3.8.3 + + com.google.code.gson + gson + 2.8.2 + + diff --git a/src/main/java/de/hsel/spm/baudas/web/Data.java b/src/main/java/de/hsel/spm/baudas/web/Data.java new file mode 100644 index 0000000..ed3581e --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/Data.java @@ -0,0 +1,39 @@ +package de.hsel.spm.baudas.web; + +import lombok.Getter; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ +public class Data { + + @Getter + private static ConcurrentMap files = new ConcurrentHashMap<>(); + + + public static Path add(String name) { + Path path; + if(files.size() >= 5) { + path = null; + files.put(UUID.randomUUID(), name); + }else { + UUID uuid = UUID.randomUUID(); + files.put(uuid, name); + path = Paths.get("target/" + uuid + ".csv"); + } + return path; + } + + public static File get(int index) { + return new File("target/" + index + ".csv"); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/Files.java b/src/main/java/de/hsel/spm/baudas/web/Files.java new file mode 100644 index 0000000..56ff232 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/Files.java @@ -0,0 +1,29 @@ +package de.hsel.spm.baudas.web; + +import com.google.gson.Gson; +import org.jetbrains.annotations.NotNull; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ + +@WebServlet("/files") +public class Files extends HttpServlet { + + @Override + protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException { + resp.setContentType("application/json"); + PrintWriter out = resp.getWriter(); + Gson gson = new Gson(); + out.println(gson.toJson(Data.getFiles())); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/Upload.java b/src/main/java/de/hsel/spm/baudas/web/Upload.java new file mode 100644 index 0000000..5da00e2 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/Upload.java @@ -0,0 +1,49 @@ +package de.hsel.spm.baudas.web; + +import org.jetbrains.annotations.NotNull; + +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.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ + +@WebServlet("/upload") +@MultipartConfig +public class Upload extends HttpServlet { + + @Override + protected void doPost(@NotNull HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding(StandardCharsets.UTF_8.name()); + + Part filePart = req.getPart("file"); + String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); + InputStream inputStream = filePart.getInputStream(); + + + Path path = Data.add(fileName); + System.out.println(path); + if(!Files.exists(path)) { + Files.createFile(path); + } + + Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING); + + resp.sendRedirect("files"); + } +} \ No newline at end of file diff --git a/src/main/webapp/version.jsp b/src/main/webapp/version.jsp new file mode 100644 index 0000000..d47f7ea --- /dev/null +++ b/src/main/webapp/version.jsp @@ -0,0 +1,7 @@ +<%@ page import="weka.core.Version" %> +Server info: <%= application.getServerInfo() %>
+Servlet version: <%= application.getMajorVersion() %>.<%= application.getMinorVersion() %>
+JSP version: <%= JspFactory.getDefaultFactory().getEngineInfo().getSpecificationVersion() %>
+Java version: <%= System.getProperty("java.version") %>
+ +Weka version: <%= Version.VERSION %> \ No newline at end of file