本文介绍了Spring - 在浏览器中显示PDF文件而不是下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用spring在浏览器中显示pdf。
我的问题是浏览器下载文件而不是显示它。
这是我的代码:
i am trying to display a pdf in my browser with spring.my problem is that the browser downloads the file instead of displaying it.this is my code:
@RequestMapping(value="/getpdf1", method=RequestMethod.GET)
public ResponseEntity<byte[]> getPDF1() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
String filename = "pdf1.pdf";
headers.add("content-disposition", "inline;filename=" + filename);
headers.setContentDispositionFormData(filename, filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(pdf1Bytes, headers, HttpStatus.OK);
return response;
}
我期待你的回答!
谢谢
i am looking forward to your answers!thanks
推荐答案
你差不多完成了。只需删除
You are almost done. Just remove
headers.setContentDispositionFormData(filename, filename);
。将multipart / form-data发布到服务器时应该使用它。
from your code block. It should be used when posting multipart/form-data to the server.
这篇关于Spring - 在浏览器中显示PDF文件而不是下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!