如何使用servlet和JSP显示PDF文件? [英] How do I display a PDF file using servlets and JSP?

查看:260
本文介绍了如何使用servlet和JSP显示PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何使用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?

推荐答案

只需从数据库中以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.
// ...

当映射到例如/pdfservleturl-pattern时,则可以使用<a>链接

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>

<object>(如果要将其嵌入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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆