VUE前端通过PDF.js实现pdf文件预览,出现如下问题:
通过GET方式调用后端接口,后端返回文件流实现文件预览,
前端代码:
let url = `http://xxxx:8503/policyquery/v1/renewalPdfFilePreview?reviewLink=${newUrl}`; console.log(url) this.pdfSrc = `static/pdf/web/viewer.html?file=${encodeURIComponent(url)}`;后端代码:
String strUrl = reviewLink.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)"); httpURLConnection.setConnectTimeout(30000); httpURLConnection.setReadTimeout(30000); // 取得输入流,并使用Reader读取 input = (InputStream) httpURLConnection.getInputStream(); response.getOutputStream().write(IOUtils.toByteArray(input)); 后端接口通过response 来实现文件流的输出。就可能会出现java.io.IOException: 您的主机中的软件中止了一个已建立的连接问题 解决方案: 在response的Header头中添加参数
response.setHeader("Access-Control-Allow-Origin","*"); 便可以解决! 问题页面:
正常页面: