【问题标题】:I can´t open a .pdf in my browser by Java我无法通过 Java 在浏览器中打开 .pdf
【发布时间】:2011-06-29 18:32:45
【问题描述】:

我正在尝试打开我在浏览器中使用 iText 库创建的 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 但不起作用。我很绝望。

谢谢。

现在,我正在尝试这种方式,但它不起作用:

公共类 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);
    }
}

}

【问题讨论】:

    标签: java pdf browser


    【解决方案1】:

    您将 mimetype 指定为 application/text,而它应该是 application/pdf

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        改变

         response.setContentType("application/text");
        

        response.setContentType("application/pdf");
        

        如果您希望您的 pdf 在浏览器中打开,请进行以下更改

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

        【讨论】:

        • 显示的修改无效。它不会给我一个错误但不起作用。有什么建议?会不会是 out Stream 的选择不正确?
        • 可能..控制台有异常吗?
        • 不。我在原始帖子中以另一种方式发布我现在尝试的方式。
        • 我建议将您的代码与this 进行比较,只需替换上面提到的响应参数
        【解决方案4】:

        将文件名放在双引号"

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

        【讨论】:

        • 如果附件名称包含双引号,这仍然会受到标题拆分。
        【解决方案5】:

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

        【讨论】:

        • 你能提供一个来源吗?我很确定 Android 使用的 Webkit 版本可以正确处理问题 POST,并正确解释缓存控制标头。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-02
        • 2011-06-12
        • 2014-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多