elcharrua
elcharrua

Reputation: 1682

Java Spark REST api upload file

I'm working with java using java-spark to create the Rest Api and I'm having trouble figuring out how to receive a file so then I can process it. Haven't found anything as like in Spring that handles MultipartFile. Also this proyect is ran on a Tomcat server.

Upvotes: 0

Views: 1082

Answers (1)

Mithfindel
Mithfindel

Reputation: 4708

As per the official documentation, the following code you get you started:

post("/yourUploadPath", (request, response) -> {
    request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
    try (InputStream is = request.raw().getPart("uploaded_file").getInputStream()) {
        // Use the input stream to create a file
    }
    return "File uploaded";
});

Upvotes: 2

Related Questions