亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法創建生成的 HTTP 客戶端所需的返回類型,因為沒有從 ByteBuffer

無法創建生成的 HTTP 客戶端所需的返回類型,因為沒有從 ByteBuffer

牧羊人nacy 2023-09-20 17:03:32
下面是使用 micronaut 將文件作為休息響應發送到客戶端的服務器端代碼。@Get(value = "/downloadFile", produces = MediaType.APPLICATION_OCTET_STREAM )public HttpResponse<File> downloadDocument() throws IOException {    File sampleDocumentFile = new File(getClass().getClassLoader().getResource("SampleDocument.pdf").getFile());    return HttpResponse.ok(sampleDocumentFile).header("Content-Disposition", "attachment; filename=\"" + sampleDocumentFile.getName() + "\"" );}下面是調用上述端點的客戶端。@Client(value = "/client")public interface DownloadDocumentClient {@Get(value = "/downloadDocument", processes = MediaType.APPLICATION_OCTET_STREAM)public Flowable<File> downloadDocument();}嘗試檢索文件如下:-Flowable<File> fileFlowable = downloadDocumentClient.downloadDocument();    Maybe<File> fileMaybe = fileFlowable.firstElement();    return fileMaybe.blockingGet();獲取異常為io.micronaut.context.exceptions.ConfigurationException:無法創建生成的 HTTP 客戶端所需的返回類型,因為沒有注冊從 ByteBuffer 到類 java.io.File 的 TypeConverter
查看完整描述

1 回答

?
幕布斯7119047

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();


查看完整回答
反對 回復 2023-09-20
  • 1 回答
  • 0 關注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號