Skip to content

Instantly share code, notes, and snippets.

View eaiman-shoshi's full-sized avatar
🔥
Glitch

Eaiman Shoshi eaiman-shoshi

🔥
Glitch
View GitHub Profile
package com.masterdevskills.cha1;
public class Account {
private int balance;
public Account(int balance) {
this.balance = balance;
}
public void transfer (int value, Account account) {
@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")
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<>();
}
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);
})
// 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);
}