pdf.js 跨域访问远程服务器地址

最近项目要求在线显示pdf,一顿搜索之后感觉pdf.js还不错,但是一直存在一个问题:跨域访问,pdf.js不能直接跨域,最后用后台服务将url的pdf转成byte[],然后用pdf.js解析data,这样就成功解决跨域问题了,

解析url

  InputStream inputStream = null;
        try {
            String strUrl = url.trim();
            URL url = new URL(strUrl);
            URLConnection connection = url.openConnection();
            HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
            httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            inputStream = httpURLConnection.getInputStream();
            BufferedInputStream    bin = new BufferedInputStream(inputStream);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BufferedOutputStream     bout = new BufferedOutputStream(baos);
            byte[] buffer = new byte[1024];
            int len = bin.read(buffer);
            while (len != -1) {
                bout.write(buffer, 0, len);
                len = bin.read(buffer);
            }
           
            bout.flush();
            byte[] bytes = baos.toByteArray();
             return bytes
        } catch (IOException e) {
        }
复制代码

pdf.js 加载data

loadingTask = pdfjsLib.getDocument({data:data});//pdf.js的function用来代替url 
复制代码

转载于:https://juejin.im/post/5c749aa4e51d4569943b9213

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值