1 回答

TA貢獻1794條經驗 獲得超8個贊
您無法使用實例發送文件數據File,因為它僅包含路徑而不包含文件內容。您可以使用字節數組發送文件內容。
以這種方式更新控制器:
@Get(value = "/download", produces = MediaType.APPLICATION_OCTET_STREAM)
public HttpResponse<byte[]> downloadDocument() throws IOException, URISyntaxException {
String documentName = "SampleDocument.pdf";
byte[] content = Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource(documentName).toURI()));
return HttpResponse.ok(content).header("Content-Disposition", "attachment; filename=\"" + documentName + "\"");
}
那么客戶端將會是這樣的:
@Get(value = "/download", processes = MediaType.APPLICATION_OCTET_STREAM)
Flowable<byte[]> downloadDocument();
最后客戶致電:
Flowable<byte[]> fileFlowable = downloadDocumentClient.downloadDocument();
Maybe<byte[]> fileMaybe = fileFlowable.firstElement();
byte[] content = fileMaybe.blockingGet();
更新: 如果您需要將接收到的字節(文件內容)保存到客戶端計算機(容器)上的文件中,那么您可以這樣做,例如:
Path targetPath = Files.write(Paths.get("target.pdf"), fileMaybe.blockingGet());
如果您確實需要實例File而不是Path進一步處理,那么只需:
File file = targetPath.toFile();
添加回答
舉報