【发布时间】:2022-01-25 12:47:00
【问题描述】:
@GetMapping(value = "/pdf",produces= MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<InputStreamResource> getTermsConditions() throws Exception {
String filePath = "C:\\";
String fileName = "PDF.pdf";
File file = new File(filePath+fileName);
HttpHeaders headers = new HttpHeaders();
// headers.add("content-disposition", "inline;filename=" +fileName);
headers.add("Content-Disposition", "attachment; filename=" + fileName);
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
return ResponseEntity.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/pdf"))
.body(resource);
}
打开文件,但格式不正确。
���� 6 0 obj > endobj 7 0 obj > endobj 10 0 obj
请帮助我。我想在在线图书馆之类的浏览器中打开图书pdf
【问题讨论】:
-
我得到同样的错误!
-
它适用于我(尽管它下载 PDF 而不是在浏览器中显示它)。你能更好地描述发生了什么吗? “打开文件”是什么意思?以下输出显示在什么程序中?你使用的是什么浏览器?您能否还显示 HTTP 请求的详细信息(例如浏览器检查器的屏幕截图)。
标签: spring spring-boot web-services pdf response-entity