diff --git a/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java b/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java index 21a3193158ea..2c6f73bfbfcb 100644 --- a/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java +++ b/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java @@ -24,7 +24,9 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; +import java.util.stream.Collectors; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import org.springframework.context.ApplicationContext; @@ -249,13 +251,10 @@ public Mono> getMultipartData() { public Mono cleanupMultipart() { return Mono.defer(() -> { if (this.multipartRead) { - return getMultipartData() - .onErrorComplete() - .flatMapIterable(Map::values) - .flatMapIterable(Function.identity()) - .flatMap(part -> part.delete() - .onErrorComplete()) - .then(); + return Mono.usingWhen(getMultipartData().onErrorComplete().map(this::collectParts), + parts -> Mono.empty(), + parts -> Flux.fromIterable(parts).flatMap(part -> part.delete().onErrorComplete()) + ); } else { return Mono.empty(); @@ -263,6 +262,10 @@ public Mono cleanupMultipart() { }); } + private List collectParts(MultiValueMap multipartData) { + return multipartData.values().stream().flatMap(List::stream).collect(Collectors.toList()); + } + @Override public LocaleContext getLocaleContext() { return this.localeContextResolver.resolveLocaleContext(this);