This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.masterdevskills.cha1; | |
public class Account { | |
private int balance; | |
public Account(int balance) { | |
this.balance = balance; | |
} | |
public void transfer (int value, Account account) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PostMapping(value = "/upload-multiValueMap", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_STREAM_JSON_VALUE) | |
@ResponseStatus(value = HttpStatus.OK) | |
public Mono<YourResponseClass> uploadFileMap(@RequestBody Mono<MultiValueMap<String, Part>> filePartMapMono) { | |
return filePartMapMono.flatMap(this::sendFile); | |
} | |
private Mono<YourResponseClass> sendFile (MultiValueMap<String, Part> filePartMap) { | |
return webClient.post() | |
.uri("YOUR_ENDPOINT") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private List<String> processAndGetLinesAsList(String string) { | |
Supplier<Stream<String>> streamSupplier = string::lines; | |
var isFileOk = streamSupplier.get().allMatch(line -> line.matches(MultipartFileUploadUtils.REGEX_RULES)); | |
return isFileOk ? streamSupplier.get().collect(Collectors.toList()) : new ArrayList<>(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Flux<String> getLines(Flux<FilePart> filePartFlux) { | |
return filePartFlux.flatMap(filePart -> | |
filePart.content().map(dataBuffer -> { | |
byte[] bytes = new byte[dataBuffer.readableByteCount()]; | |
dataBuffer.read(bytes); | |
DataBufferUtils.release(dataBuffer); | |
return new String(bytes, StandardCharsets.UTF_8); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use Flux<FilePart> for multiple file upload | |
@PostMapping(value = "/upload-flux", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_STREAM_JSON_VALUE) | |
@ResponseStatus(value = HttpStatus.OK) | |
public Flux<String> upload(@RequestPart("files") Flux<FilePart> filePartFlux) { | |
return uploadService.getLines(filePartFlux); | |
} |