我正在撰寫一個需要從另一個 Web 服務器回傳 PDF 檔案的方法:
@RequestMapping(value = "download", method = RequestMethod.GET)
public ResponseEntity<Resource> download(String document) throws Exception {
String file = "http://blabla.com?document=" document;
HttpHeaders headers = new HttpHeaders();
headers.add("content-disposition", "attachment; filename=" document);
headers.setContentType(MediaType.APPLICATION_PDF);
UrlResource u = new UrlResource(new URL(file));
return ResponseEntity.ok().headers(headers).body(u);
}
這似乎有效。然而,PDF 可能會變得很大,而且我不完全確定如何ResponseEntity.body()讀取和寫入檔案。
此處是否將完整資料讀入記憶體?如果是這樣,我如何設法讓它流式傳輸?
我知道如何使用 HttpServletResponse 并寫入輸出流,但我想知道 Spring 是否已經在這里處理了這個問題。
uj5u.com熱心網友回復:
Spring 會為您解決這個問題。Resource通過從資源的輸入流中讀取并寫入回應的輸出流,將回應流式傳輸到客戶端。如果您想查看代碼,請在ResourceHttpMessageConverter.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/449448.html
上一篇:物體持久化是否填充id欄位
