我想預覽一個用java生成的pdf檔案,但是下面的代碼給出了這個錯誤
無法識別的回應型別;將內容顯示為文本。
@GetMapping("/previewPDF/{codeStudent}")
public ResponseEntity<byte[]> previewPDF(@PathVariable("codeStudent") String code) throws IOException {
byte[] pdf = //pdf content in bytes
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "inline; filename=" "example.pdf");
headers.setContentType(MediaType.parseMediaType("application/pdf"));
return ResponseEntity.ok().headers(headers).body(pdf);
}
更新:這是錯誤的螢屏截圖

uj5u.com熱心網友回復:
您需要為您的資源指定回應 PDF 媒體型別。
請參閱RFC 標準。媒體型別的完整串列。
關于產生 Media Type
的 Spring 檔案。
@GetMapping(value = "/previewPDF/{codeStudent}", produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> previewPDF(@PathVariable("codeStudent") String code) throws IOException
還為您設定 PDF 內容型別ResponseEntity
@GetMapping(value = "/previewPDF/{codeStudent}", produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> previewPDF(@PathVariable("codeStudent") String code) throws IOException {
byte[] pdf = null;
HttpHeaders headers = new HttpHeaders();
String fileName = "example.pdf";
headers.setContentDispositionFormData(fileName, fileName);
headers.setContentType(MediaType.APPLICATION_PDF);
return ResponseEntity.ok().headers(headers).body(pdf);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/450492.html
