Upload saves files in correct order.
#SPM-7: add work 1h development
This commit is contained in:
parent
fbcd9e913f
commit
b50a490cd4
|
@ -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>");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,15 +1,18 @@
|
||||||
package de.hsel.spm.baudas.web;
|
package de.hsel.spm.baudas.web;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import org.jetbrains.annotations.Contract;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -17,23 +20,35 @@ import java.util.concurrent.ConcurrentMap;
|
||||||
public class Data {
|
public class Data {
|
||||||
|
|
||||||
@Getter
|
@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;
|
Path path;
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
if(files.size() >= 5) {
|
if(files.size() >= 5) {
|
||||||
path = null;
|
SavedFile file = files.poll();
|
||||||
files.put(UUID.randomUUID(), name);
|
if(!get(file.getUuid()).delete()) {
|
||||||
|
System.out.println("failed");
|
||||||
|
}
|
||||||
|
path = Paths.get(getFileName(uuid));
|
||||||
|
files.offer(new SavedFile(uuid, name));
|
||||||
}else {
|
}else {
|
||||||
UUID uuid = UUID.randomUUID();
|
files.add(new SavedFile(uuid, name));
|
||||||
files.put(uuid, name);
|
path = Paths.get(getFileName(uuid));
|
||||||
path = Paths.get("target/" + uuid + ".csv");
|
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File get(int index) {
|
@Contract
|
||||||
return new File("target/" + index + ".csv");
|
@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";
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -9,8 +9,11 @@ import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* list all currently available files.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -22,6 +25,7 @@ public class Files extends HttpServlet {
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException {
|
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException {
|
||||||
resp.setContentType("application/json");
|
resp.setContentType("application/json");
|
||||||
|
resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||||
PrintWriter out = resp.getWriter();
|
PrintWriter out = resp.getWriter();
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
out.println(gson.toJson(Data.getFiles()));
|
out.println(gson.toJson(Data.getFiles()));
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -18,6 +18,8 @@ import java.nio.file.Paths;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* saves uploaded files.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue