0

Sending a POST request (Apache httpclient, here Kotlin source code):

val httpPost = HttpPost("http://localhost:8000")
val builder = MultipartEntityBuilder.create()
builder.addBinaryBody("file", File("testFile.zip"),
        ContentType.APPLICATION_OCTET_STREAM, "file.ext")
val multipart = builder.build()
httpPost.entity = multipart
val r = httpClient.execute(httpPost)
r.close()

I receive the request in my post handler as a via spark-java Request-object. How do I retrieve the original file (plus the file name as a bonus) from the post request? The request.bodyAsBytes() method seems to add some bytes because the body is larger than the original file.

Thanks, Jörg

osx
  • 146
  • 1
  • 11

1 Answers1

0

Near the bottom of Spark's Documentation page there is a section "Examples and FAQ". The first example is "How do I upload something?". From there, it links further to an example on GitHub.

In short:

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

To access the original file name:

request.raw().getPart("file").getSubmittedFileName()

To handle multiple files or parts, I usually have code similar to the following (assuming only files are included in the multi-part encoded upload):

for (Part part : req.raw().getParts()) {
  try (InputStream stream = part.getInputStream()) {
    String filename = part.getSubmittedFileName();
    // save the input stream to the filesystem, and the filename to a database
  }
}
cello
  • 5,356
  • 3
  • 23
  • 28
  • Thanks for the reply but sadly the example does not work because the req.raw().getParts() or req.raw().getPart("file") simply end the execution of the post handler. I traced the call and somewhere, deep in spark-java, the POST request is marked as handled. The handler is still active but will not be executed beyond the getParts() or getPart(). I'm new to Kotlin therefore I'm not sure if it is an issue of the spark-java Kotlin port. – osx Oct 04 '17 at 18:35
  • If the execution suddenly stops, it's most likely due to an exception. Use `Spark.exception(Exception.class, (e, request, response) -> { e.printStackTrace(); });` as an additional route to make sure you see exceptions (sorry, Java code again, don't know Kotlin). – cello Oct 04 '17 at 18:41
  • Thanks again for the reply. The JetBrains IDE converted the code to `Spark.exception(Exception::class.java, { e, request, response -> e.printStackTrace() })` and I was able to find an error (no multipart message). Now the code boils down to the (edited) code, giving me a `java.lang.NullPointerException` because the `getParts()` returns an empty list, hence the exception for `getPart("file")`. The content type of the POST request is `multipart/form-data; boundary=Z2Dl0386ZA0OMWccx1VRo6h7emmRriXNz`. – osx Oct 04 '17 at 19:09