PDF文件预览 简单实现

java后端代码

/**
     * (HttpClient)爬虫获取各种资源pdf文件 跳ssl验证
     * @param response
     * @param dataJson pdf 文件路径
     */
    @GetMapping("/pdfFile")
    public void getFile(HttpServletResponse response, String dataJson) {
        service.pdfview(response,dataJson);
    }
 @Override
    public void pdfview(HttpServletResponse response, String dataJson) {
        if( dataJson==null || "".equals(dataJson) || "null".equals(dataJson)) {
            return;
        }
        String str = "";
        List<Map<String,String>> list = mapper.getFileUrl(dataJson);
        if (list!=null && list.size()>0) {
            Map<String,String> mapList = list.get(0);
            str = mapList.get("URL");
        }
        // 这里可根据自身的需要返回的文件类型调整消息投类型  灵活一点也可做为参数
        response.setContentType("application/pdf");
       // JSONObject object = JSONObject.parseObject(dataJson);
       // String pdfUrl = object.getString("pdfUrl");
        InputStream inputStream = null;
        BufferedInputStream bins = null;
        OutputStream outs = null;
        BufferedOutputStream bouts = null;
        try {
            File file = new File(str);
            inputStream = new BufferedInputStream(new FileInputStream(file));
           // inputStream = HttpUtils.getYCFile("D:\\抢修周报.pdf"); // 使用爬虫获取文件流
            if(inputStream == null) {
                return;
            }
            bins = new BufferedInputStream(inputStream);//放到缓冲流里面
            outs = response.getOutputStream(); // 获取输出流
            bouts = new BufferedOutputStream(outs); //放到缓冲流里面

            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            //开始向网络传输文件流
            while ((bytesRead = bins.read(buffer, 0, 1024)) != -1) {
                bouts.write(buffer, 0, bytesRead);
            }
            bouts.flush();//这里一定要调用flush()方法

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (bins != null) {
                    bins.close();
                }
                if (outs != null) {
                    outs.close();
                }
                if (bouts != null) {
                    bouts.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

工具类

package com.alonginfo.important.server.utils;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class SslUtils {
    private static void trustAllHttpsCertificates() throws Exception {
        TrustManager[] trustAllCerts = new TrustManager[1];
        TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    }

    static class miTM implements TrustManager,X509TrustManager {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public boolean isServerTrusted(X509Certificate[] certs) {
            return true;
        }

        public boolean isClientTrusted(X509Certificate[] certs) {
            return true;
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }
    }

    /**
     * 忽略HTTPS请求的SSL证书,必须在openConnection之前调用
     * @throws Exception
     */
    public static void ignoreSsl() throws Exception{
        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
                return true;
            }
        };
        trustAllHttpsCertificates();
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
    }
}

前端代码

<template>
  <main>
  <el-button type="danger" @click="pdfview">预览</el-button>
  <el-dialog
    title="附件管理"
    :visible.sync="pdfdialogVisible"
    :before-close="closeDialogspdf"
    width="60%"
    height="300px"
  >
    <iframe class="prism-player" :src="url" width="100%" style=" height: 90vh;"></iframe>
  </el-dialog>
  </main>
</template>

<script>
export default {
  data () {
    return {
      url: '',
      pdfdialogVisible: false
    }
  },

  created () {

  },
  methods: {
    pdfview () {
      this.pdfdialogVisible = true
      this.url = 'http://localhost:8888/important_server/fileOps/pdfFile?dataJson=12157f94332b47418ad111cb541e4efd'
    },
    closeDialogspdf () {
      this.pdfdialogVisible = false
    }
  }

}
</script>

转载修改自此链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值