实现在线查看pdf word功能记录

1.在线查看pdf

前端:主要使用了pdfJs,后端仅需使用 

HttpServletResponse获取流输出对象(OutputStream)对前端进行响应即可
 //点击查看公司经营备案凭证
        $("#supplierBusinessCertificate").click(function (){
            window.open("${pageContext.request.contextPath}/js/pdfJs/web/viewer.html?file="
                +encodeURIComponent("${pageContext.request.contextPath}/supplierInfo/selSupplierBusinessCertificateById?supplierId="+ supplier.supplierId +""));
        })

后端:

 @GetMapping("/selSupplierBusinessLicenseById")
    public void selSupplierBusinessLicenseById(Long supplierId,HttpServletRequest request, HttpServletResponse response) {
        SupplierInfo byId = supplierInfoService.getOne(new QueryWrapper<SupplierInfo>()
                .select("SUPPLIER_BUSINESS_LICENSE").eq("SUPPLIER_ID",supplierId));

        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + byId.getSupplierName()+"的营业执照.pdf");

        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            System.out.println("==================pdf处理开始==================");
            os = response.getOutputStream();
            //获得PDF文件流,核心代码
            InputStream is = new ByteArrayInputStream(byId.getSupplierBusinessLicense());
            System.out.println("获取流结束。。。。");
            bis = new BufferedInputStream(is);

            int i = 0;
            while ((i = bis.read(buff)) != -1) {
                os.write(buff, 0, i);
                os.flush();

            }

        } catch (Exception e) {
            System.out.println("pdf处理出现异常:" + e.getMessage() + "; ");
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

2.查看文档

同样使用pdfJs即可,基本同上,这种方法适用于我的项目。

 $("#wenhao2").click(function (){
            layer.msg("请稍等")
            window.open("${pageContext.request.contextPath}/js/pdfJs/web/viewer.html?file="
                +encodeURIComponent("${pageContext.request.contextPath}/supplierInfo/getSuperviseType"));
        })
@GetMapping("/getSuperviseType")
    public void getSuperviseType(HttpServletResponse response) throws IOException {
        System.out.println("查看医学装备目录doc文档被访问");
//        File file = new File("C:\\Users\\Administrator\\Desktop\\医学装备 分类目录.pdf");
        File file = new File("/home/xb/tomcat8/webapps/pdf/医学装备 分类目录.pdf");

        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=医学装备 分类目录.pdf");

        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;

        try {
            System.out.println("==================pdf处理开始==================");
            os = response.getOutputStream();

            FileInputStream fileInputStream = new FileInputStream(file);
//            fileInputStream.read

            //获得PDF文件流,核心代码
//            InputStream is = new ByteArrayInputStream();
//            System.out.println("获取流结束。。。。");
            bis = new BufferedInputStream(fileInputStream);

            int i = 0;
            while ((i = fileInputStream.read(buff)) != -1) {
                os.write(buff, 0, i);
                os.flush();

            }

        } catch (Exception e) {
            System.out.println("pdf处理出现异常:" + e.getMessage() + "; ");
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        System.out.println("查看医学装备目录doc文档完成");
    }

本文仅用于记录我的代码,若能帮助到您,不胜荣幸

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值