您的位置:首页 > 编程语言 > Java开发

java 在线查看本地pdf文档或者图片

2017-11-24 12:14 836 查看

----在java中,用浏览器查看pdf文档或者图片,亦或者其他一些文件,有很多种方法,下面介绍一种以流的方式查看pdf的方法

    @RequestMapping(value = "/getPdfPath")

    public String getpic(String path, HttpServletRequest request, HttpServletResponse response) throws IOException {

        File file = new File(path);

        if (!file.exists()) {

            request.setAttribute("error", "附件已删除或不存在");

            //      return "/error";

        }

        InputStream in = null;

        OutputStream os = null;

        try {

            response.setContentType("application/pdf"); // 设置返回内容格式

            in = new FileInputStream(file);   //用该文件创建一个输入流

            os = response.getOutputStream();  //创建输出流

            byte[] b = new byte[1024];

            while (in.read(b) != -1) {

                os.write(b);

            }

            in.close();

            os.flush();

            os.close();

        } catch (Exception e) {

            try {

                if (null != in) {

                    in.close();

                }

            } catch (IOException e1) {

                e1.printStackTrace();

            }

            try {

                if (null != os) {

                    os.close();

                }

            } catch (IOException e2) {

                e2.printStackTrace();

            }

        }

        return null;

    }

查看其他文件 ,查询api修改文件返回格式

------效果图如下




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pdf