Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SparkJava: Upload file did't work in Spark java framework

Tags:

java

I have got some method from the stackoverflow about uploading file in spark java, but I try and did't work.

post("/upload",
          (request, response) -> {

            if (request.raw().getAttribute("org.eclipse.jetty.multipartConfig") == null) {
                MultipartConfigElement multipartConfigElement = new MultipartConfigElement(System.getProperty("java.io.tmpdir"));
                request.raw().setAttribute("org.eclipse.jetty.multipartConfig", multipartConfigElement);
            }
            Part file = request.raw().getPart("file");
            Part name = request.raw().getPart("name");
            String filename = file.getName();
            if(name.getSize() > 0){
                try{
                    filename = IOUtils.toString(name.getInputStream(), StandardCharsets.UTF_8);
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
            Path filePath = Paths.get(".",filename);
            Files.copy(file.getInputStream(),filePath);
            return "Done!";
          });

}

I use postman to send the message

enter image description here

And I got the Error like this

enter image description here

The error points to the code Part file = request.raw().getPart("file");

like image 321
L. YanJun Avatar asked Jan 12 '16 14:01

L. YanJun


People also ask

How do I upload files to spark?

The Spark API accepts file uploads as a MIME upload in the same way your web browser would upload a file in a web form. The two most important aspects are 1) to name the field to which you send "files" and 2) to set your Content-Type header to be multipart/form-data including a boundary.

Does spark use Jetty?

Spark is a compact framework for building web applications that run on the JVM. It comes with an embedded web server, Jetty, so you can get started in minutes.

How do I stop Java spark Server?

Stopping the Server By calling the stop() method the server is stopped and all routes are cleared.


1 Answers

post("/upload", "multipart/form-data", (request, response) -> {

String location = "image";          // the directory location where files will be stored
long maxFileSize = 100000000;       // the maximum size allowed for uploaded files
long maxRequestSize = 100000000;    // the maximum size allowed for multipart/form-data requests
int fileSizeThreshold = 1024;       // the size threshold after which files will be written to disk

MultipartConfigElement multipartConfigElement = new MultipartConfigElement(
     location, maxFileSize, maxRequestSize, fileSizeThreshold);
 request.raw().setAttribute("org.eclipse.jetty.multipartConfig",
     multipartConfigElement);

Collection<Part> parts = request.raw().getParts();
for (Part part : parts) {
   System.out.println("Name: " + part.getName());
   System.out.println("Size: " + part.getSize());
   System.out.println("Filename: " + part.getSubmittedFileName());
}

String fName = request.raw().getPart("file").getSubmittedFileName();
System.out.println("Title: " + request.raw().getParameter("title"));
System.out.println("File: " + fName);

Part uploadedFile = request.raw().getPart("file");
Path out = Paths.get("image/" + fName);
try (final InputStream in = uploadedFile.getInputStream()) {
   Files.copy(in, out);
   uploadedFile.delete();
}
// cleanup
multipartConfigElement = null;
parts = null;
uploadedFile = null;

return "OK";
});

This will work well, I found it in https://groups.google.com/forum/#!msg/sparkjava/fjO64BP1UQw/CsxdNVz7qrAJ

like image 103
L. YanJun Avatar answered Oct 13 '22 18:10

L. YanJun