gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:46:35 27 4
gpt4 key购买 nike

我正在尝试使用 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 将强制其始终下载。

关于java - SpringBoot - 在浏览器中显示 PDF 文件而不是下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53619895/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com