使用 webclient webflux 将文件发布到接受 APPLICATION_OCTET_STREAM 的 API

Nir*_*ane 2 spring-boot spring-webflux spring-webclient

我想使用 Webflux Web 客户端将文件上传到 Rest api。接受文件的 API 具有内容类型,APPLICATION_OCTET_STREAM我如何使用 spring 来做到这一点WebClient

我怎样才能将下面的curl转换为WebClient请求?

curl -i -X POST --header "Content-Type:application/octet-stream" --header --data-binary @myfile.pdf  https://some-host/some-url
Run Code Online (Sandbox Code Playgroud)

注意- 我不期望有很大的文件

Nir*_*ane 5

这就是我上传文件的方式

步骤 1 :将文件读取为字节数组

bytes = new FileSystemResource((filePath)).getInputStream()
                    .readAllBytes();
Run Code Online (Sandbox Code Playgroud)

步骤 2 :将字节数组传递给bodyValue方法

webClient.post()
         .uri("Some-uri)
         .contentType(MediaType.APPLICATION_OCTET_STREAM)
         .bodyValue(bytes)
         .retrieve()
         .toBodilessEntity()) 
Run Code Online (Sandbox Code Playgroud)