Upload saves files in correct order.

#SPM-7: add work 1h development
This commit is contained in:
Johannes Theiner 2019-05-03 13:06:21 +02:00
parent fbcd9e913f
commit b50a490cd4
6 changed files with 57 additions and 56 deletions

View File

@ -1,45 +0,0 @@
package de.hsel.spm.baudas;
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;
/**
* Hello World.
*
* @author Johannes Theiner
* @version 0.1
* @since 0.1
*/
@WebServlet("/")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 156790693L;
/**
* doGet.
*
* @param req Request
* @param resp Response
* @throws IOException failed
*/
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
final PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hallo Welt!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hallo Ostfriesland!</h1>");
out.println("</body>");
out.println("</html>");
}
}

View File

@ -1,15 +1,18 @@
package de.hsel.spm.baudas.web;
import lombok.Getter;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
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;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
*
*
* @author Johannes Theiner
* @version 0.1
* @since 0.1
@ -17,23 +20,35 @@ import java.util.concurrent.ConcurrentMap;
public class Data {
@Getter
private static ConcurrentMap<UUID, String> files = new ConcurrentHashMap<>();
private static ConcurrentLinkedQueue<SavedFile> files = new ConcurrentLinkedQueue<>();
public static Path add(String name) {
@NotNull
static Path add(@NotNull String name) {
Path path;
UUID uuid = UUID.randomUUID();
if(files.size() >= 5) {
path = null;
files.put(UUID.randomUUID(), name);
SavedFile file = files.poll();
if(!get(file.getUuid()).delete()) {
System.out.println("failed");
}
path = Paths.get(getFileName(uuid));
files.offer(new SavedFile(uuid, name));
}else {
UUID uuid = UUID.randomUUID();
files.put(uuid, name);
path = Paths.get("target/" + uuid + ".csv");
files.add(new SavedFile(uuid, name));
path = Paths.get(getFileName(uuid));
}
return path;
}
public static File get(int index) {
return new File("target/" + index + ".csv");
@Contract
@NotNull public static File get(@NotNull UUID uuid) {
return new File(getFileName(uuid));
}
@Contract(pure = true)
@NotNull
private static String getFileName(@NotNull UUID uuid) {
return "target/" + uuid + ".csv";
}
}

View File

@ -9,8 +9,11 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
/**
* list all currently available files.
*
* @author Johannes Theiner
* @version 0.1
* @since 0.1
@ -22,6 +25,7 @@ public class Files extends HttpServlet {
@Override
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException {
resp.setContentType("application/json");
resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
PrintWriter out = resp.getWriter();
Gson gson = new Gson();
out.println(gson.toJson(Data.getFiles()));

View File

@ -0,0 +1,14 @@
package de.hsel.spm.baudas.web;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.UUID;
@Getter
@AllArgsConstructor
public class SavedFile {
private UUID uuid;
private String name;
}

View File

@ -18,6 +18,8 @@ import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
/**
* saves uploaded files.
*
* @author Johannes Theiner
* @version 0.1
* @since 0.1

View File

@ -0,0 +1,11 @@
<!doctype html>
<html>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">
Hochladen
</button>
</form>
</body>
</html>