【java】servlet输出pdf文件到浏览器 教程

把这个项目部署成你的web项目 http://pan.baidu.com/share/link?shareid=136974&uk=436295647项目下载

打开谷歌浏览器,输入http://localhost:8080/fileOutDemo/outFile

注意

1.谷歌浏览器内嵌pdf的功能,不用重新安装pdf阅读器就能看效果。

2.如果不是谷歌浏览器,请事先安装浏览器的pdf阅读器插件(比如福昕阅读器,就可以把pdf内嵌到浏览器)

Ok 没了,研究一下代码吧,很简单。但是网上目前还真心的不好找,网上的一般就是下载文件到本地,那样的话,对于用户来说是一件很不爽的事情,用户不爽,领导就要求我们自己做一个,于是就自己试着做了一个,感觉速度或者性能都挺快的。


 效果:


代码

/**

 * 两种方法输出pdf到浏览器,你可以参考一下百度,试试输出excel到浏览器直接打开

 */

packagecom.liu.servlet;

importjava.io.BufferedInputStream;

importjava.io.BufferedOutputStream;

importjava.io.FileNotFoundException;

importjava.io.IOException;

importjava.io.PrintWriter;

importjava.net.URL;

importjavax.servlet.ServletException;

importjavax.servlet.ServletOutputStream;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

public classOutFile extendsHttpServlet {

    public voiddoGet(HttpServletRequest  request, HttpServletResponse response) throwsServletException,  IOException {

        doPost(request, response);

    }

    public voiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

        // 向浏览器端输出

        response.setCharacterEncoding("utf-8");

        response.setContentType("application/pdf");

        String rootPath = this.getServletContext().getRealPath("/") + "port\\";

        String pdfFileDesc = rootPath + "pdf\\2012年北京地铁线路图.pdf";

        // servlet输出流

        ServletOutputStream outr = null;

        // 传送的文件的url地址

        outr = response.getOutputStream();

         

        try{

            outPdf2(pdfFileDesc, outr);

        } catch(IOException e) {

            // 处理文件找不到的情况

            try{

                response.reset();

                response.setContentType("text/html;charset=gb2312");

                response.getWriter().println("文件未找到");

            } catch(IOException e1) {

                e1.printStackTrace();

            }

            e.printStackTrace();

        } finally{

            if(outr != null) {

                outr.close();

            }

            System.out.println("pdf顺利输出,请给我的帖子留言");

        }

    }

    /**

     * 输出pdf到浏览器,采用BufferedInputStream和BufferedOutputStream,轻松实战IO流,熟悉servlet输出文件到浏览器而不是下载的Demo

     * @param pdfFileDesc

     * @param outr

     * @throws IOException

     */

    public voidoutPdf(String pdfFileDesc,  ServletOutputStream outr) throwsIOException {

        // 输入流

        BufferedInputStream bis = null;

        // 输出流

        BufferedOutputStream bos = null;

        URL url = null;

        try{

            url = newURL("file:\\"+ pdfFileDesc);

            // 从文件获得输入流

            bis = newBufferedInputStream(url.openStream());

            // 输出到servlet

            bos = newBufferedOutputStream(outr);

            // 利用缓冲数组

            byte[] buff = new byte[2048];

            intbytesRead = 0;

            // 一个简单的读写循环

            while(-1 != (bytesRead =  bis.read(buff, 0, buff.length))) {

                bos.write(buff, 0,  bytesRead);

                outr.flush();

            }

        } finally{

            if(bis != null) {

                try{

                    bis.close();

                } catch(IOException e) {

                    // TODOAuto-generated catch block

                    e.printStackTrace();

                }

            }

            if(bos != null) {

                try{

                    bos.close();

                } catch(IOException e) {

                    // TODOAuto-generated catch block

                    e.printStackTrace();

                }

            }

        }

    }

    /**

     * 第二种方法 输出pdf到浏览器,采用BufferedInputStream和BufferedOutputStream,轻松实战IO流,熟悉servlet输出文件到浏览器而不是下载的Demo

     * @param pdfFileDesc

     * @param outr

     * @throws IOException

     */

    public voidoutPdf2(String pdfFileDesc,  ServletOutputStream outr) throwsIOException {

        // 输入流

        BufferedInputStream bis = null;

        // 输出流

        BufferedOutputStream bos = null;

        URL url = null;

        try{

            url = newURL("file:\\"+ pdfFileDesc);

            // 从文件获得输入流

            bis = newBufferedInputStream(url.openStream());

            // 输出到servlet

            bos = newBufferedOutputStream(outr);

            // 利用缓冲数组

            byte[] buffer = new byte[2048];

            intlen = 0;

            // 另一个简单的读写循环

            while((len = bis.read(buffer)) !=  -1) {

                outr.write(buffer, 0, len); // write to client

                outr.flush();

            }

        } finally{

            if(bis != null) {

                try{

                    bis.close();

                } catch(IOException e) {

                    e.printStackTrace();

                }

            }

            if(bos != null) {

                try{

                    bos.close();

                } catch(IOException e) {

                    e.printStackTrace();

                }

            }

        }

    }

}

QQ 394263788 欢迎探讨java知识

原文地址:https://www.cnblogs.com/ae6623/p/4416613.html