Skip to content

Commit 1a54133

Browse files
committed
Refactor multipart upload
1 parent 7447996 commit 1a54133

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

‎spring-5/src/main/java/com/baeldung/functional/FormHandler.java‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.baeldung.functional;
22

3+
import org.springframework.core.io.buffer.DataBuffer;
34
import org.springframework.util.MultiValueMap;
45
import org.springframework.web.reactive.function.server.ServerRequest;
56
import org.springframework.web.reactive.function.server.ServerResponse;
67
import reactor.core.publisher.Mono;
78

9+
import java.util.List;
810
import java.util.concurrent.atomic.AtomicLong;
911

1012
import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers;
@@ -28,13 +30,15 @@ Mono<ServerResponse> handleUpload(ServerRequest request) {
2830
return request
2931
.body(toDataBuffers())
3032
.collectList()
31-
.flatMap(dataBuffers -> {
32-
AtomicLong atomicLong = new AtomicLong(0);
33-
dataBuffers.forEach(d -> atomicLong.addAndGet(d
34-
.asByteBuffer()
35-
.array().length));
33+
.flatMap(dataBuffers -> ok()
34+
.body(fromObject(extractData(dataBuffers).toString())));
35+
}
3636

37-
return ok().body(fromObject(atomicLong.toString()));
38-
});
37+
private AtomicLong extractData(List<DataBuffer> dataBuffers) {
38+
AtomicLong atomicLong = new AtomicLong(0);
39+
dataBuffers.forEach(d -> atomicLong.addAndGet(d
40+
.asByteBuffer()
41+
.array().length));
42+
return atomicLong;
3943
}
4044
}

0 commit comments

Comments
 (0)