fileUploadAll
Description
Simple access to streams of bytes for all files uploaded in a multipart form together with metadata about each upload.
If there is no field with the given name the request will be rejected.
Note
This directive buffers all files to temporary files on disk in files prefixed akka-http-upload
. This is to work around limitations of the HTTP multipart format. To upload only one file it may be preferred to use the fileUpload directive, as it streams the file directly without buffering.
Example
final Route route = extractRequestContext(ctx -> {
return fileUploadAll("csv", byteSources -> {
// accumulate the sum of each file
CompletionStage<Integer> sumF = byteSources.stream()
.map(item -> {
// sum the numbers as they arrive
return item.getValue().via(Framing.delimiter(
ByteString.fromString("\n"), 1024))
.mapConcat(bs -> Arrays.asList(bs.utf8String().split(",")))
.map(s -> Integer.parseInt(s))
.runFold(0, (acc, n) -> acc + n, ctx.getMaterializer());
})
.reduce(CompletableFuture.completedFuture(0), (accF, intF) -> {
return accF.thenCombine(intF, (a, b) -> a + b);
});
return onSuccess(() -> sumF, sum -> complete("Sum: " + sum));
});
});
Map<String, String> filenameMappingA = new HashMap<>();
Map<String, String> filenameMappingB = new HashMap<>();
filenameMappingA.put("filename", "primesA.csv");
filenameMappingB.put("filename", "primesB.csv");
akka.http.javadsl.model.Multipart.FormData multipartForm =
Multiparts.createStrictFormDataFromParts(
Multiparts.createFormDataBodyPartStrict("csv",
HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8,
"2,3,5\n7,11,13,17,23\n29,31,37\n"), filenameMappingA),
Multiparts.createFormDataBodyPartStrict("csv",
HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8,
"41,43,47\n53,59,61,67,71\n73,79,83\n"), filenameMappingB));
// test:
testRoute(route).run(HttpRequest.POST("/").withEntity(
multipartForm.toEntity(BodyPartRenderer.randomBoundaryWithDefaults())))
.assertStatusCode(StatusCodes.OK)
.assertEntityAs(Unmarshaller.entityToString(), "Sum: 855");