spring-mvc - 如何使用 Spring Web 应用程序显示 PDF 文件
问题描述
我希望在页面上显示此文件,但此代码可直接下载
<a th:href="@{/pdf/Manjaro-User-Guide.pdf}">Show Pdf file</a>
我正在使用 Spring-Thymeleaf
谢谢!
解决方案
我通过评论下面的行找到了解决方案
//response.setHeader("Content-Disposition", "attachment; filename=\"demo.pdf\"");
这是代码示例:
@GetMapping(value = "/pdf")
public void showPDF(HttpServletResponse response) throws IOException {
response.setContentType("application/pdf");
//response.setHeader("Content-Disposition", "attachment; filename=\"demo.pdf\"");
InputStream inputStream = new FileInputStream(new File(rootLocation + "/Manjaro-User-Guide.pdf"));
int nRead;
while ((nRead = inputStream.read()) != -1) {
response.getWriter().write(nRead);
}
}
谢谢!
推荐阅读
- javascript - 如何获取我的复选框内联旁边和复选框旁边的文本?
- woocommerce - 在结账时用相机扫描信用卡 - Woocommerce & Stripe
- firebase - 在 Firebase 云功能中指定 RAM
- reactjs - React 路由器仅适用于 URL 中的 /#/,否则在 Ubuntu 服务器上会出现错误 404
- r - 添加循环以读取多个pdf页面时R中的语法错误
- python - 是否可以使用 Tkinter 创建适用于 Android 或 iOS 的应用程序?
- reactjs - 将正弦波包示例转换为反应组件
- c# - 如何在应用程序中使用多个 openGL 状态?
- javascript - 如何在当前 html 页面上显示来自 textarea 评论框的信息
- python - Python - Pandas - 忽略空格后的数据