2012-11-09 26 views
2

我正在尋找一個PDF文檔從數據庫顯示到瀏覽器,我希望瀏覽器打開它,但它也可以,如果它的提示下載。我知道這個問題已經在這裏和其他論壇提出過,但我仍然沒有贏得這項任務。如何使用Servlet和JSP顯示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; 
    } 


    } 

方法從數據庫中檢索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錯誤。

+2

我很抱歉,但我錯過了你的實際問題。你有什麼問題? – Robert

+0

@Robert問題是,當我點擊JSP頁面上的按鈕時,它給我一個404錯誤。 –

+0

檢查服務器日誌是否有錯誤,首先確定它是否正在擊中你的servlet? –

回答

1

謝謝大家。我設法解決了這個問題。我的錨並沒有在目錄中找到servlet。這是下面

修復前:

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

後:

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

感謝。