+ Cleanup + Documentation Signed-off-by: Johannes Theiner <j.theiner@live.de> #SPM-11: add work 2h development #SPM-11: add work 30m documentation
61 lines
1.7 KiB
Java
61 lines
1.7 KiB
Java
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;
|
|
|
|
/**
|
|
* saves uploaded files.
|
|
*
|
|
* @author Johannes Theiner
|
|
* @version 0.1
|
|
* @since 0.1
|
|
**/
|
|
|
|
@WebServlet("/upload")
|
|
@MultipartConfig
|
|
public class Upload extends HttpServlet {
|
|
|
|
private static final long serialVersionUID = 14144111845151L;
|
|
|
|
/**
|
|
* doPost.
|
|
*
|
|
* @param req request
|
|
* @param resp response
|
|
* @throws ServletException could not get file part
|
|
* @throws IOException writer could not be retrieved
|
|
*/
|
|
@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);
|
|
if (!Files.exists(path)) {
|
|
Files.createFile(path);
|
|
}
|
|
|
|
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
resp.sendRedirect("");
|
|
}
|
|
} |