繁体   English   中英

SpringBoot-在浏览器中显示PDF文件,而不是下载

[英]SpringBoot - display PDF-file in browser instead of downloading

我正在尝试使用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()));

}

我期待着您的答复! 谢谢

将标题更改为此:

Content-Type: application/pdf
Content-Disposition: inline; filename="filename.pdf"

将Content-Type设置为application/octet-stream将强制其始终下载。

使用Content-Type application/pdf代替application/octet-stream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM