java 如何使用 Servlet 和 JSP 显示 PDF 文档?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13310271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Display a PDF document with a Servlet and JSP?
提问by Siya Sosibo
I looking to display a PDF document from a database to a browser, I wish the browser to open it but it's also ok if its prompt for it to be download. I know this question has been asked here and other forums but I'm still not winning with this task.
我希望将 PDF 文档从数据库显示到浏览器,我希望浏览器打开它,但如果它提示下载它也可以。我知道这里和其他论坛上有人问过这个问题,但我仍然没有赢得这项任务。
I have looked at these: JSP n ServletsDisplay PDF via JSP n Servlet tutorial
我看过这些: JSP n Servlets Display PDF via JSP n Servlet tutorial
My current code.
我当前的代码。
OBJ/Entity:``
对象/实体:``
public class Attach
{
private String filename = null;
private InputStream attach = null;
private int ContentLength = 0;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public InputStream getAttach() {
return attach;
}
public void setAttach(InputStream attach) {
this.attach = attach;
}
public int getContentLength() {
return ContentLength;
}
public void setContentLength(int contentLength) {
ContentLength = contentLength;
}
public Attach() {
}
public Attach(String filename, InputStream attach) {
this.attach = attach;
this.filename = filename;
}
}
Method to retrieve PDF from DB:
从数据库检索PDF的方法:
public Attach getPDFData(String filename) {
try {
pstmt = conn.prepareStatement("SELECT * FROM FUNC_AREA WHERE FILE_NME = ?");
pstmt.setString(1, filename);
rs = pstmt.executeQuery();
if (rs.next()) {
newattach.setFilename(rs.getString(3));
newattach.setAttach(rs.getBinaryStream(5));
}
} catch (SQLException e) {
e.printStackTrace();
}
return newattach;
}
My Servlet:
我的小服务程序:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
showAttach(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
showAttach(request, response);
}
public void showAttach(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RepoOperations repops = new RepoOperations();
Attach newattachobj = repops.getPDFData("bond.pdf");
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int inputStreamLength = 0;
int length = 0;
while ((length = newattachobj.getAttach().read(buffer)) > 0) {
inputStreamLength += length;
baos.write(buffer, 0, length);
}
if (inputStreamLength > newattachobj.getContentLength()) {
newattachobj.setContentLength(inputStreamLength);
}
if (response instanceof HttpServletResponse) {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.reset();
httpResponse.setHeader("Content-Type", "application/pdf");
httpResponse.setHeader("Content-Length", String.valueOf(newattachobj.getContentLength()));
httpResponse.setHeader("Content-Disposition", "inline; filename=\"" + newattachobj.getFilename() + "\"");
}
response.getOutputStream().write(baos.toByteArray(), 0, (int)newattachobj.getContentLength());
//finally
response.getOutputStream().flush();
//clear
baos = null;
System.out.println(newattachobj.getFilename());
} finally {
// TODO Auto-generated catch block
close(response.getOutputStream());
close(newattachobj.getAttach());
}
}
private void close(Closeable resource) throws IOException {
if (resource != null) {
resource.close();
}
}
JSP:
JSP:
<form action="ShowAttach">
<a href="ShowAttach">click here</a>
<br/>
<input type="submit" value="submit" id="submit">
</form>
I'm looking to have a Hyperlink on the JSP page to open the PDF document.Thank You
我希望在 JSP 页面上有一个超链接来打开 PDF 文档。谢谢
The problem is that when I click the button on the JSP page, it gives me a 404 error.
问题是当我点击 JSP 页面上的按钮时,它给了我 404 错误。
采纳答案by Siya Sosibo
Thanks everyone. I managed to solve the issue. My anchor wasn't finding the servlet in the directory. This was the fix below
感谢大家。我设法解决了这个问题。我的锚没有在目录中找到 servlet。这是下面的修复
Before:
前:
<a href="ShowAttach">Open</a>
After:
后:
<a href="../ShowAttach">Open</a>
Thanks.
谢谢。