Spring Webflux上传大图像文件并以流方式通过WebClient发送文件 [英] Spring Webflux Upload Large Image File and Send The File with WebClient in Streaming Way

查看:188
本文介绍了Spring Webflux上传大图像文件并以流方式通过WebClient发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring webflux功能样式.我想创建一个接受大图像文件的终结点,并通过流客户端将该文件发送到webClient的另一服务.

I am using spring webflux functional style. I want to create a endpoint which accepts large image files and send this files to another service with webClient in streaming way.

所有文件处理都应该以流方式进行,因为我不想因为内存不足而使应用程序崩溃.

All file processing should be in streaming way because I don't want to my app crush because of outofmemory.

反正有这样做吗?

推荐答案

可能是这样的:

  @PostMapping(value = "/images/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public Mono<ResponseEntity<Void>> uploadImages(@RequestPart("files") Flux<FilePart> fileParts) {
    return fileParts
        .flatMap(filePart -> {
          return webClient.post()
              .uri("/someOtherService")
              .body(BodyInserters.fromPublisher(filePart.content(), DataBuffer.class))
              .exchange()
              .flatMap(clientResponse -> {
                //some logging
                return Mono.empty();
              });
        })
        .collectList()
        .flatMap(response -> Mono.just(ResponseEntity.accepted().build()));
  }

这接受MULTIPART FORM DATA,您可以在其中附加多个图像文件并将其上传到另一服务.

This accepts MULTIPART FORM DATA where you can attach multiple image files and upload them to another service.

这篇关于Spring Webflux上传大图像文件并以流方式通过WebClient发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆