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>