2011-02-18 86 views
1

我試圖打開我在瀏覽器中使用iText庫創建的pdf,但它失敗。 這是代碼I'm用來發送到瀏覽器我不能在我的瀏覽器中通過Java打開.pdf

File file = new File(path); 

    try{ 
     //InputStream stream=blob.getBinaryStream(); 
     InputStream streamEntrada = new FileInputStream(file); 
     //ServletOutputStream fileOutputStream = response.getOutputStream(); 
     PrintWriter print = response.getWriter(); 

     int ibit = 256; 
     while ((ibit) >= 0) 
     { 
      ibit = streamEntrada.read(); 
      print.write(ibit); 
     } 

     response.setContentType("application/text"); 
     response.setHeader("Content-Disposition", "attachment;filename="+name); 
     response.setHeader("Pragma", "cache"); 
     response.setHeader("Cache-control", "private, max-age=0"); 
     streamEntrada.close(); 
     print.close(); 

     return null; 
    } 
    catch(Exception e){ 

     return null; 
    } 
} 

我用FileOutputStream中,但isn't作品嚐試。我很絕望。

謝謝。

現在,我正嘗試這種方式,但它doesn't工作:

公共類MovilInfoAction擴展DOWNLOADACTION {

protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 

    //Here the creation of the PDF 

    //Storing data 
    PdfData dPdf = pdf.drawPDF(terminal); 

    String path = dPdf.getPath();//Path 
    String name = dPdf.getName()+".pdf";//Pdf´s name 
    String contentType = "application/pdf";//ContentType 

    response.setContentType(contentType); 
    response.setHeader("Content-Disposition","attachment; filename="+name); 
    response.setHeader("Cache-control", "private, max-age=0"); 
    response.setHeader("Content-Disposition", "inline"); 

    File file = new File(path); 
    byte[] pdfBytes = es.vodafone.framework.utils.Utils.getBytesFromFile(file); 

    return new ByteArrayStreamInfo(contentType, pdfBytes); 
} 

protected class ByteArrayStreamInfo implements StreamInfo { 

    protected String contentType; 
    protected byte[] bytes; 

    public ByteArrayStreamInfo(String contentType, byte[] bytes) { 
     this.contentType = contentType; 
     this.bytes = bytes; 
    } 

    public String getContentType() { 
     return contentType; 
    } 

    public InputStream getInputStream() throws IOException { 
     return new ByteArrayInputStream(bytes); 
    } 
} 

}

回答

3

您指定的MIME類型爲application/text,當它應該是application/pdf

+0

我正在嘗試這種方式: – user623020 2011-02-21 10:18:36

3

在寫入數據之前,應該設置Header和ContentType。 然後將內容類型設置爲application/pdf

3

變化

response.setContentType("application/text"); 

response.setContentType("application/pdf"); 

,如果你想你的PDF在瀏覽器中打開然後進行以下更改

response.setHeader("Content-Disposition", "inline"); 
+0

由於修改指示不起作用。它不會給我一個錯誤,但不起作用。有什麼建議麼?它可能是一個錯誤的選擇流出? – user623020 2011-02-21 09:48:48

+0

可能是..控制檯上是否有任何異常? – 2011-02-21 09:54:14

+0

沒有。我以另一種方式發佈我如何在原始帖子中嘗試。 – user623020 2011-02-21 10:24:02

0

把文件名中的雙引號"

response.setHeader("Content-Disposition","attachment; filename=\"" + attachmentName + "\""); 
0

Android默認瀏覽器需要GET請求。它不理解POST請求,因此無法下載附件。您可以通過發送GET請求發送GET請求,它解決了我的問題。 Android瀏覽器自行生成GET請求並將其發回服務器。即使GET請求首次由servlet發送,第二次請求後收到的響應也會被瀏覽器視爲最終響應。

相關問題