1. 程式人生 > 其它 >VUE前端實現PDF預覽時出現org.apache.catalina.connector.ClientAbortException:java.io.IOException: 您的主機中的軟體中止了一個已建立的連線

VUE前端實現PDF預覽時出現org.apache.catalina.connector.ClientAbortException:java.io.IOException: 您的主機中的軟體中止了一個已建立的連線

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","*");

便可以解決!

問題頁面:

正常頁面: