我正在尝试使用 spring 在我的浏览器中显示 pdf。 我的问题是浏览器下载文件而不是显示它。 这是我的代码:
@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;
}
期待您的回答! 谢谢
最佳答案
Content-Disposition 用于控制下载或在浏览器中查看
在浏览器中查看
headers.add("Content-Disposition", "inline; filename=" + "example.pdf");
下载
headers.add("Content-Disposition", "attachment; filename=" + "example.pdf");
关于Spring - 在浏览器中显示 PDF 文件而不是下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35480463/