我不能在我的浏览器中通过Java打开.pdf

问题描述:

我试图打开我在浏览器中使用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); 
    } 
} 

}

您指定的MIME类型为application/text,当它应该是application/pdf

+0

我正在尝试这种方式: – user623020 2011-02-21 10:18:36

在写入数据之前,应该设置Header和ContentType。 然后将内容类型设置为application/pdf

变化

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

把文件名中的双引号"

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

Android默认浏览器需要GET请求。它不理解POST请求,因此无法下载附件。您可以通过发送GET请求发送GET请求,它解决了我的问题。 Android浏览器自行生成GET请求并将其发回服务器。即使GET请求首次由servlet发送,第二次请求后收到的响应也会被浏览器视为最终响应。