Closed andrea-bologna closed 8 years ago
File uploadDir = new File("upload");
uploadDir.mkdir(); // create the upload directory if it doesn't exist
staticFiles.externalLocation("upload");
get("/", (req, res) ->
"<form method='post' enctype='multipart/form-data'>"
+ " <input type='file' name='uploaded_file' accept='.png'>"
+ " <button>Upload picture</button>"
+ "</form>"
);
post("/", (req, res) -> {
Path tempFile = Files.createTempFile(uploadDir.toPath(), "", "");
req.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
try (InputStream input = req.raw().getPart("uploaded_file").getInputStream()) {
Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING);
}
return "<h1>You uploaded this image:<h1><img src='" + tempFile.getFileName() + "'>";
});
Added to docs: http://sparkjava.com/documentation.html#examples
Hello, is there a simple way to implement a file uplaod?