我正在尝试使用spring在浏览器中显示pdf。我的问题是浏览器下载文件而不是显示文件。这是我的代码:
@RequestMapping(value = "/download" , method=RequestMethod.GET , produces = "application/pdf")
public ResponseEntity<InputStreamResource> downloadPdf() throws IOException {
ClassPathResource pdfFile = new ClassPathResource("TMOBILE.pdf");
return ResponseEntity
.ok()
.contentLength(pdfFile.contentLength())
.contentType(
MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(pdfFile.getInputStream()));
}
我期待您的回答!谢谢
答案 0 :(得分:2)
将标题更改为此:
Content-Type: application/pdf
Content-Disposition: inline; filename="filename.pdf"
将内容类型设置为application/octet-stream
将强制其始终下载。
答案 1 :(得分:0)
使用内容类型application/pdf
代替application/octet-stream