如何使用 servlet 和 JSP 显示 PDF 文件? [英] How do I display a PDF file using servlets and JSP?
问题描述
谁能告诉我如何使用 servlet 和 JSP 显示存储在我的数据库中的 PDF 文件?有没有具体的jar文件需要导入?
Can anyone tell how to display a PDF file which is stored in my database using servlets and JSP? Are there any specific jar files to be imported?
推荐答案
只需从 DB 中获取 InputStream
并将其写入响应的 OutputStream
沿着正确的集合的标题.这是假设您使用 JDBC 与 DB 交互的片段.
Just get it as InputStream
from DB and write it to OutputStream
of the response along a correct set of headers. Here's a snippet assuming you're using JDBC to interact with DB.
response.setContentType("application/pdf");
// ...
InputStream input = resultSet.getBinaryStream("columnname");
OutputStream output = response.getOutputStream();
// Write input to output the usual way.
// ...
当映射到例如 /pdfservlet
的 url-pattern
时,您可以使用 <a>
链接调用 servlet
When mapped on an url-pattern
of for example /pdfservlet
, then you can just call the servlet using <a>
link
<a href="pdfservlet?id=123">click here</a>
或者 如果你想把它嵌入到 HTML 中.
or by <object>
if you want to embed it in HTML.
<object data="pdfservlet?id=123" type="application/pdf" width="600" height="400">
</object>
您不需要任何额外的库.可以在此处找到完整的启动示例.
You don't need any additional libraries. A complete kickoff example can be found here.
这篇关于如何使用 servlet 和 JSP 显示 PDF 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!