SSL配置:
package com.example.mybatisplus.utils.ssl;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class MITM implements TrustManager, X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType){
return;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType){
}
}
工具类:
import com.example.mybatisplus.utils.ssl.MITM;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wxd
* Date: 2018-09-27
* Time: 17:17
*/
public class DownloadUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(DownloadUtil.class);
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
private static void trustAllHttpsCertificates() throws Exception {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new MITM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
/**
* 支持Https 下载文件
* @param httpUrl 链接
* @param swapStream 输出流
* @throws Exception 异常
*/
public static void httpsEncode(String httpUrl,OutputStream swapStream) throws Exception {
byte[] data;
InputStream in;
HttpURLConnection http;
trustAllHttpsCertificates();
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
http = (HttpURLConnection) (new URL(httpUrl).openConnection());
setHttpURLConnection(http);
String contentEncoding = http.getContentEncoding();
if("gzip".equalsIgnoreCase(contentEncoding)){
in = new GZIPInputStream(http.getInputStream());
}else{
in = http.getInputStream();
}
data = new byte[in.available()];
int rc = 0;
while((rc = in.read(data)) > 0){
swapStream.write(data, 0, rc);
}
//关闭连接和流
http.disconnect();
in.close();
if(swapStream != null){
swapStream.close();
}
}
private static void setHttpURLConnection(HttpURLConnection http){
http.setRequestProperty("accept", "text/html, application/xhtml+xml, image/jxr, */*");
http.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0");
http.setRequestProperty("content-type", "application/x-www-form-urlencoded");
}
/**
* 支持Https 下载到指定的文件
* @param httpUrl 下载链接
* @param file 空文件,只需要指定一个文件名称,方便创建临时文件。
* @throws Exception 异常
*/
public static void httpsEncode(String httpUrl,File file) throws Exception {
OutputStream swapStream = new FileOutputStream(file);
httpsEncode(httpUrl,swapStream);
}
/**
* 浏览器下载文件
* @param response 响应
* @param downloadUrl 下载链接
* @param savename 浏览器默认保存的文件名
* @throws Exception 异常
*/
public static void download(HttpServletResponse response,String downloadUrl,String savename) throws Exception {
setDownloadInfo(response,savename);
ServletOutputStream out = response.getOutputStream();
httpsEncode(downloadUrl,out);
out.close();
}
/**
* 下载文件工具方法
* @param response 响应流
* @param savename 浏览器默认保存的文件名
*/
public static void setDownloadInfo(HttpServletResponse response,String savename){
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-disposition",
"attachment; filename=" + new String(savename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
}
// //批量下载
// public static void downloadZip(HttpServletResponse response, List<String> fileIds, String savename) throws Exception {
// response.setCharacterEncoding("UTF-8");
//
// response.setContentType("application/octet-stream;charset=UTF-8");
//
// File zipfile = TempFileUtils.createTempFile("temp.zip");
// //文件名
// if(StringUtils.isEmpty(savename)){
// savename = "资料";
// }
// savename = savename + ".zip";
// response.setHeader("Content-disposition",
// "attachment; filename=" + new String(savename.getBytes("utf-8"), "ISO8859-1"));
// List<File> zipfiles = new ArrayList<>();
// for(String fileid : fileIds){
//
// String name = fileDownLoadResponse.getName();
// //文件创建
// File file = TempFileUtils.createTempFile(name);
// String down_url = fileDownLoadResponse.getAuthrequest().get(1);
// httpEncode(down_url,file);
// zipfiles.add(file);
//
// LOGGER.info("创建成功:-->"+file.getAbsolutePath());
// }
// ZipUtil.zipFiles(zipfiles,zipfile);
// ServletOutputStream outputStream = response.getOutputStream();
// //把文件写入输出流
// FileInputStream fis = new FileInputStream(zipfile);
// byte[] buffer = new byte[10240];
// int size = 0;
// while((size = fis.read(buffer))!=-1){
// outputStream.write(buffer, 0, size);
// }
//
// //关闭流
// fis.close();
// if(outputStream != null){
// outputStream.close();
// }
// //删掉文件
// TempFileUtils.deleteTempFile(zipfile);
// for(File file : zipfiles){
// TempFileUtils.deleteTempFile(file);
// LOGGER.info("删除成功:-->"+file.getAbsolutePath());
// }
// }
}