【问题标题】:How to Display a PDF document with a Servlet and JSP?如何使用 Servlet 和 JSP 显示 PDF 文档?
【发布时间】:2012-10-29 21:39:50
【问题描述】:

我希望将 PDF 文档从数据库显示到浏览器,我希望浏览器打开它,但如果它提示下载它也可以。我知道这个问题已经在这里和其他论坛被问过,但我仍然没有完成这项任务。

我看过这些: JSP n Servlets Display PDF via JSP n Servlet tutorial

我当前的代码。

OBJ/实体:``

 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;
    }


    }

从 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;
}

我的 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:

 <form action="ShowAttach">
    <a href="ShowAttach">click here</a>
    <br/>
    <input type="submit" value="submit" id="submit">
    </form>

我希望在 JSP 页面上有一个超链接来打开 PDF 文档。谢谢

问题是当我点击 JSP 页面上的按钮时,它给了我一个 404 错误。

【问题讨论】:

  • 很抱歉,我错过了您的实际问题。你有什么问题?
  • @Robert 问题是当我点击 JSP 页面上的按钮时,它给了我一个 404 错误。
  • 检查服务器日志是否有错误,首先确定它是否命中了你的servlet?
  • 嘿[这个链接][1]可能对你有很大帮助[1]:stackoverflow.com/a/7386373/1069633
  • 这里最好的文件 servlet 示例之一:balusc.blogspot.ca/2009/02/…

标签: java jsp pdf servlets oracle10g


【解决方案1】:

谢谢大家。我设法解决了这个问题。我的锚在目录中没有找到 servlet。这是下面的修复

之前:

<a href="ShowAttach">Open</a>

之后:

<a href="../ShowAttach">Open</a>

谢谢。

【讨论】:

    猜你喜欢
    • 2011-06-04
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2015-05-20
    • 1970-01-01
    • 2011-08-10
    相关资源
    最近更新 更多