I'm currently working on Spring WebFlux. I'm trying to upload large file (70mo) using Spring WebFlux.
My Controller
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Flux<String> uploadHandler(@RequestBody Flux<Part> fluxParts, @RequestParam(value = "categoryType") String categoryType, @PathVariable(value = "traceabilityReportUuid") String traceabilityUuid) {
return documentHandler.upload(fluxParts, UUID.fromString(traceabilityUuid), categoryType);
}
My Service
public Flux<String> upload(Flux<Part> fluxParts, UUID traceabilityUuid, String categoryType) {
return fluxParts
.filter(part -> part instanceof FilePart)
.ofType(FilePart.class)
.flatMap(p -> this.upload(p, traceabilityUuid, categoryType));
}
private Mono<String> upload(FilePart filePart, UUID traceabilityUuid, String categoryType) {
return filePart.content().collect(InputStreamCollector::new, (t, dataBuffer) -> t.collectInputStream(dataBuffer.asInputStream()))
.flatMap(inputStreamCollector -> {
upload(traceabilityUuid, inputStreamCollector.getInputStream(), filePart.filename(), categoryType);
return Mono.just("OK");
});
}
My Collector
public class InputStreamCollector {
private InputStream is;
public void collectInputStream(InputStream is) {
if (this.is == null) this.is = is;
this.is = new SequenceInputStream(this.is, is);
}
public InputStream getInputStream() {
return this.is;
}
}
And at the end, I retrieve the full inputstream by this way : inputStreamCollector.getInputStream()
and pass to my object.
And I use this object in order to send to bucket S3.
But before sending to S3, I must to convert it into file (using apache tools), I have a stackoverflow exception.
java.lang.StackOverflowError: null
at java.base/java.io.SequenceInputStream.read(SequenceInputStream.java:156)
at java.base/java.io.SequenceInputStream.read(SequenceInputStream.java:156)
at java.base/java.io.SequenceInputStream.read(SequenceInputStream.java:156)
at java.base/java.io.SequenceInputStream.read(SequenceInputStream.java:156)
at java.base/java.io.SequenceInputStream.read(SequenceInputStream.java:156)
at java.base/java.io.SequenceInputStream.read(SequenceInputStream.java:156)
at java.base/java.io.SequenceInputStream.read(SequenceInputStream.java:156)
at java.base/java.io.SequenceInputStream.read(SequenceInputStream.java:156)
it works fine with a small file (7mo ..)
Do you have an idea to resolve my issue please ?
Finally I found the solution !
https://github.com/entzik/reactive-spring-boot-examples/blob/master/src/main/java/com/thekirschners/springbootsamples/reactiveupload/ReactiveUploadResource.java
I adapted the code in order to return an InputStream and it works fine with large files ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With