2013-01-07 36 views
3

我想在瀏覽器中顯示PDF文件。我有通往JS的pdf的路徑,我正在調用從Java獲取PDF作爲servlet。這是我到目前爲止有:使用servlet在瀏覽器中顯示PDF

的JavaScript:

RequestManager.getJSON(Config.server + "getPDF.json?pdfPath=" + this.pathToPdfFile, (function(data){ 
     $("#" + this.divId).append('<object id="' + this.pdfObjectId + '" data="' + data + '" type="application/pdf" width="600" height="800"></object>'); 
     ResizeManager.addResizeHandler(this.pdfObjectId, this.divId, -10, -10); 
    }).bind(this)); 

的Java:

@RequestMapping("/getPDF") 
public void pdfPathToServlet(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{ 
    String pdfPath = request.getParameter("pdfPath"); 
    if (pdfPath == null || pdfPath.equals("")) 
     throw new ServletException("Invalid or non-existent file parameter in UrlServlet servlet."); 

    if (pdfPath.indexOf(".pdf") == -1) 
     pdfPath += ".pdf"; 

    File pdf = new File(pdfPath); 
    String pdfName = pdfPath.substring(pdfPath.lastIndexOf("/") + 1, pdfPath.length()); 
    logger.debug(pdfName); 
    ServletOutputStream stream = null; 
    BufferedInputStream buf = null; 
    try 
    { 
     stream = response.getOutputStream(); 
     response.setContentType("application/pdf"); 
     response.setHeader("Content-Disposition", "inline; filename='" + pdfName + "'"); 
     FileInputStream input = new FileInputStream(pdf); 
     response.setContentLength((int) pdf.length()); 
     buf = new BufferedInputStream(input); 
     int readBytes = 0; 
     while ((readBytes = buf.read()) != -1) 
      stream.write(readBytes); 
    } 
    catch (IOException ioe) 
    { 
     throw new ServletException(ioe.getMessage()); 
    } 
    finally 
    { 
     if (stream != null) 
      stream.close(); 
     if (buf != null) 
      buf.close(); 
    } 
} 

我的問題是,這是表示我的瀏覽器文本二進制輸出

我不知道我在做什麼不正確。我試圖將標題更改爲附件而不是內聯,但顯示的是相同的內容。我相信我想要內聯,因爲我希望在瀏覽器中顯示它,而不是下載它。

+0

爲什麼當你返回'application/pdf'時,你的網址以'.json'結尾? –

+0

@ Michael-O說實話,我已經把它放在了習慣之外。應用程序編寫的方式是整個應用程序運行的一個URL。它從來沒有真正改變。 .json通常用於防止頁面實際重定向到另一個實際上不包含任何內容的URL。 – user856354

+1

這不是一個servlet。這是一個Spring MVC控制器操作。 – BalusC

回答

5

您的JavaScript部分沒有意義。您正在獲取PDF文件作爲ajax響應,然後嘗試將其設置爲<object>元素的data屬性。 data屬性必須指向一個真實的URL,而不是文件內容。修復您的JS相應:

$("#" + this.divId).append('<object id="' + this.pdfObjectId + '" data="' + Config.server + "getPDF.json?pdfPath=" + this.pathToPdfFile + '" type="application/pdf" width="600" height="800"></object>'); 

web瀏覽器大約需要在給定的URL發送適當的HTTP請求和初始化/渲染使用的Adobe Acrobat Reader插件—如果有可用的<object>元素照顧,我寧願附上<object>中的一個<a href="pdfURL">PDF</a>,這樣至少有一個優雅的下載鏈接退化。


無關到具體的問題,即Java代碼是不是一個servlet的一部分可言,但是Spring MVC的行動。我建議您直接瞭解您的條款並在our Servlets wiki page中閱讀以瞭解他們的真實身份。

+0

非常感謝。 – user856354

+0

不客氣。 – BalusC

+1

另外,我對可憐的術語表示歉意。我通常根本不在這方面工作。最近的就業變化讓我暫時陷入了這種困境,而我正面臨一個非常緊迫的期限。嘗試在全速跑向牆時挑選事物。 – user856354

-1
response.setHeader("Content-Disposition", "inline; filename='" + pdfName + "'"); 

您無法在線顯示PDF。它需要獨立在自己的頁面(或Iframe)上。

+3

這是不正確的。如果您在瀏覽器中使用內聯頭值初始化url,則PDF插件將在瀏覽器中打開,否則它將打開一個新的PDF查看器窗口。 –

+0

從AJAX調用?我不確定。 –

+0

即使'attachment'也不會有效。 JS有明顯的安全原因,沒有任何設施強制將任意內容的* Save As *對話保存在某個變量中。整個答案不適用。請注意,'inline'可以在同步請求中正常工作,前提是瀏覽器具有相應的插件(Adobe Reader)。 – BalusC

0
response.setHeader("Content-Disposition", "attachment;filename=" + pdfName); 
相關問題