我正在下載如下檔案:
private File downloadAndReturnFile(String fileId, String destination) {
log.info("Downloading file.. " fileId);
Path path = Paths.get(destination);
Flux<DataBuffer> dataBuffer = webClient.get().uri("/the/download/uri/" fileId "").retrieve()
.bodyToFlux(DataBuffer.class)
.doOnComplete(() -> log.info("{}", fileId " - File downloaded successfully"));
//DataBufferUtils.write(dataBuffer, path, StandardOpenOption.CREATE).share().block();
return ???? // What should I do here to return above DataBuffer as file?
}
如何將 dataBuffer 作為檔案回傳?或者,如何將此 dataBuffer 轉換為 File 物件?
uj5u.com熱心網友回復:
你可以使用DataBufferUtils.write方法。要做到這一點,你應該
- 實體化一個
File物件(可能使用fileIdanddestination),它也是你想要的回傳值 - 從物件創建
OutputStreamorPath或ChannelsFile物件 - 呼叫
DataBufferUtils.write(dataBuffer, ....).share().block()寫入DataBuffer檔案
即。(省略所有拋出的例外),
...
File file = new File(destination, fileId);
Path path = file.toPath();
DataBufferUtils.write(dataBuffer, path, StandardOpenOption.CREATE).share().block();
return file;
或者,
...
Path path = Paths.get(destination);
DataBufferUtils.write(dataBuffer, path, StandardOpenOption.CREATE).share().block();
return path.toFile();
uj5u.com熱心網友回復:
如果有人在尋找完整的代碼。只是最完整地分享托尼的解決方案。
private File downloadAndReturnFile(String fileId, String destination) throws FileNotFoundException {
log.info("Downloading file.. " fileId);
Flux<DataBuffer> dataBuffer = webClient.get().uri("/the/download/uri/" fileId "").retrieve()
.bodyToFlux(DataBuffer.class)
.doOnComplete(() -> log.info("{}", fileId " - File downloaded successfully"));
Path path = Paths.get(destination);
DataBufferUtils.write(dataBuffer, path, StandardOpenOption.CREATE).share().block();
return path.toFile();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/322707.html
標籤:春天 弹簧靴 弹簧-webflux 项目反应堆
