14

How to upload multiple files using Webflux?

I send request with content type: multipart/form-data and body contains one part which value is a set of files.

To process single file I do it as follow:

Mono<MultiValueMap<String, Part> body = request.body(toMultipartData());
body.flatMap(map -> FilePart part = (FilePart) map.toSingleValueMap().get("file"));

But how to done it for multiple files?

PS. Is there another way to upload a set of files in webflux ?

Vladlen Gladis
  • 1,699
  • 5
  • 19
  • 41

5 Answers5

9

I already found some solutions. Let's suppose that we send an http POST request with an parameter files which contains our files.

Note responses are arbitrary

  1. RestController with RequestPart

    @PostMapping("/upload")
    public Mono<String> process(@RequestPart("files") Flux<FilePart> filePartFlux) {
        return filePartFlux.flatMap(it -> it.transferTo(Paths.get("/tmp/" + it.filename())))
            .then(Mono.just("OK"));
    }
    
  2. RestController with ModelAttribute

    @PostMapping("/upload-model")
    public Mono<String> processModel(@ModelAttribute Model model) {
        model.getFiles().forEach(it -> it.transferTo(Paths.get("/tmp/" + it.filename())));
        return Mono.just("OK");
    }
    
    class Model {
        private List<FilePart> files;
        //getters and setters
    }
    
  3. Functional way with HandlerFunction

    public Mono<ServerResponse> upload(ServerRequest request) {
        Mono<String> then = request.multipartData().map(it -> it.get("files"))
            .flatMapMany(Flux::fromIterable)
            .cast(FilePart.class)
            .flatMap(it -> it.transferTo(Paths.get("/tmp/" + it.filename())))
            .then(Mono.just("OK"));
    
        return ServerResponse.ok().body(then, String.class);
    }
    
Vladlen Gladis
  • 1,699
  • 5
  • 19
  • 41
  • Hi! This code seems to work for an array of files sent by the client. How can we do with a client that sent a flux of files? Thanks! – Saveriu CIANELLI Oct 15 '19 at 05:37
5

You can iterate hashmap with Flux and return Flux

Flux.fromIterable(hashMap.entrySet())
            .map(o -> hashmap.get(o));

and it will be send as an array with filepart

Ricard Kollcaku
  • 1,622
  • 5
  • 9
1

the key is use toParts instead of toMultipartData, which is more simpler. Here is the example that works with RouterFunctions.

private Mono<ServerResponse> working2(final ServerRequest request) {
    final Flux<Void> voidFlux = request.body(BodyExtractors.toParts())
        .cast(FilePart.class)
        .flatMap(filePart -> {
            final String extension = FilenameUtils.getExtension(filePart.filename());
            final String baseName = FilenameUtils.getBaseName(filePart.filename());
            final String format = LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE);

            final Path path = Path.of("/tmp", String.format("%s-%s.%s", baseName, format, extension));
            return filePart.transferTo(path);
        });

    return ServerResponse
        .ok()
        .contentType(APPLICATION_JSON_UTF8)
        .body(voidFlux, Void.class);
}
nekperu15739
  • 3,311
  • 2
  • 26
  • 25
0

希望对你有帮助

@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public JSON fileUpload(@RequestPart FilePart file)throws Exception{

    OSS ossClient = new OSSClientBuilder().build(APPConfig.ENDPOINT, APPConfig.ALI_ACCESSKEYID, APPConfig.ALI_ACCESSSECRET);
    File f = null;
    String url;
    try {
        String suffix = file.filename();
        String fileName = "images/" + file.filename();
        Path path = Files.createTempFile("tempimg", suffix.substring(1, suffix.length()));
        file.transferTo(path);
        f = path.toFile();
        ossClient.putObject(APPConfig.BUCKETNAME, fileName, new FileInputStream(f));
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
        url = ossClient.generatePresignedUrl(APPConfig.BUCKETNAME, fileName, expiration).toString();
    }finally {
        f.delete();
        ossClient.shutdown();
    }
    return JSONUtils.successResposeData(url);
}
wjce
  • 1
-1

Following is the working code for uploading multiple files using WebFlux.

@RequestMapping(value = "upload", method = RequestMethod.POST)
    Mono<Object> upload(@RequestBody Flux<Part> parts) {

        return parts.log().collectList().map(mparts -> {
            return mparts.stream().map(mmp -> {
                if (mmp instanceof FilePart) {
                    FilePart fp = (FilePart) mmp;
                    fp.transferTo(new File("c:/hello/"+fp.filename()));
                } else {
                    // process the other non file parts
                }
                return mmp instanceof FilePart ? mmp.name() + ":" + ((FilePart) mmp).filename() : mmp.name();
            }).collect(Collectors.joining(",", "[", "]"));
        });

    };
vsoni
  • 2,828
  • 9
  • 13