使用java servlet在浏览器中显示Pdf [英] Display Pdf in browser using java servlet

查看:238
本文介绍了使用java servlet在浏览器中显示Pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有pdf文件。我需要在浏览器中显示pdf。我正在将文件作为fileInputStream读取,我需要在我的应用程序中使用浏览器显示pdf。但我没有pdf路径,我有文件流。

I have pdf file in my application. I need to display the pdf in browser. I am reading the file as a fileInputStream, I need to display the pdf in browser with in my application. But i dont have the pdf path, I have the file stream.

请给我一些建议和示例

我用ajax来显示pdf,我使用call_method()javascript ajax请求方法来调用showPdf动作,在showpdf动作中只是将pdf文件转换为ByteArrayOutputStream并写入输出流的结果。但它显示了以下提到的结果。

I have used ajax to display the pdf, I am using the call_method() javascript ajax request method to call the showPdf action, In showpdf action just converting the pdf file as ByteArrayOutputStream and write the result in the out put stream. But it showing the below metioned result.

JSP中的结果

% PDF-1.4% 1endstream endobj 4 0 obj< >>> / MediaBox [0 0 595 842] >> endobj 1 0 obj<> endobj 3 0 obj<> endobj 5 0 obj<> endobj 6 0 obj<> endobj xref 0 7 0000000000 65535 f 0000000389 00000 n 0000000015 00000 n 0000000477 00000 n 0000000232 00000 n 0000000540 00000 n 0000000585 00000 n预告片<< 142354f5ebefd65d6aacd33a7cb2b4ab>] / Info 6 0 R / Size 7 >> startxref 707 %% EOF

%PDF-1.4 %��� 1 endstream endobj 4 0 obj <>>>/MediaBox[0 0 595 842]>> endobj 1 0 obj <> endobj 3 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj xref 0 7 0000000000 65535 f 0000000389 00000 n 0000000015 00000 n 0000000477 00000 n 0000000232 00000 n 0000000540 00000 n 0000000585 00000 n trailer <<142354f5ebefd65d6aacd33a7cb2b4ab>]/Info 6 0 R/Size 7>> startxref 707 %%EOF

请给出一些建议。

Javascript ajax:

call_method(); <br/>
function call_method(){

    Ext.Ajax.request({
            waitMsg: 'Saving changes...',
            url:'test.action?method=showPdf',
            params : { },       
            failure:function(response,options){

            },
            success:function(response,options){
                $("#pdf_content").show();               
                $("#pdf_content").html(response.responseText);
                $("#pdf_content").show('slow');
            }
    });
}

Java方法:

public String showPdf() throws IOException {

    getResponse().setContentType("application/pdf");

    getResponse().setHeader("Content-disposition","inline; filename=automatic_start.pdf" );


    ByteArrayOutputStream baos = getByteArrayOutputStream();

    getResponse().setContentLength(baos.size());

    ServletOutputStream sos;

    sos = getResponse().getOutputStream();

    baos.writeTo(sos);

    sos.flush();

    return null;
}


private ByteArrayOutputStream getByteArrayOutputStream() throws IOException {

    String filePath = "/homefolder/";

    String folderPath=filePath+"1122/automatic_start.pdf";

    File file = new File(folderPath);

    FileInputStream fis = new FileInputStream(file);



    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[256];
     try {
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum); //no doubt here is 0
                //Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
                System.out.println("read " + readNum + " bytes,");
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        }


    return bos;
}


推荐答案

在你的servlet中,设置用于PDF的正确MIME类型: application / pdf

In your servlet, set the MIME type to the correct one for PDF : application/pdf

参见 http://www.iana.org/assignments/media-types/

这篇关于使用java servlet在浏览器中显示Pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆