如何使用 Flux<DataBuffer> 从响应中读取大文件?

sha*_*ika 5 java reactive-programming spring-boot spring-webflux spring-webclient

在我的项目中,需要从外部服务下载文件并将其存储在某个路径中。所以我RestTemplate在 spring中使用将文件下载为 Byte[] 。但是这个操作消耗了我的内存消耗。我在这个博客https://inneka.com/programming/spring/spring-webclient-how-to-stream-large-byte-to-file/ 中发现了关于使用 spring webClient 并将文件写入 path 。由于我关注的文件是我使用的音频文件APPLICATION_OCTET_STREAM。以下代码不起作用的问题。我对反应式编程非常陌生,任何对此的提示都会有所帮助。

public Mono<void> testWebClientStreaming(String filename, String format) throws IOException {
  WebClient webClient = WebClient.create("https://stackoverflow.com");
  String username = "Username";
  String token = "Password";
  String path = DOWNLOAD_TEMP_PATH + PATH_SEPERATOR + filename +"."+ format;

  Flux<DataBuffer> stream = webClient
      .get()
      .uri("/files/{filename}.{format}",
            filename, format)
      .accept(MediaType.APPLICATION_OCTET_STREAM)
      .header("Authorization", "Basic " + Base64Utils.encodeToString((username + ":" + token).getBytes(UTF_8)))
      .retrieve()
      .bodyToFlux(DataBuffer.class);

   return DataBufferUtils.write(stream, asynchronousFileChannel)
      .doOnComplete(() -> System.out.println("\n\n\n\n\n\n FINISHED  "))
      .doOnNext(DataBufferUtils.releaseConsumer())
      .doAfterTerminate(() -> {
          try {
                asynchronousFileChannel.close();
            } catch (IOException ignored) { }
       }).then();
}
Run Code Online (Sandbox Code Playgroud)

我试过exchange(),而不是retrive()工作,但我又不能在文件中写入内容。或者有什么方法可以使用其他方式实现相同的目标?