不验证网站证书下载文件

package com.jm.hsw.tools;

import java.security.cert.CertificateException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
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.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class HttpsTools {

    /**
     *     不验证网站证书下载文件
     *
     * @param urlStr  文件地址
     * @param saveDir 保存到本地的文件夹
     * @param fileName 保存到本地的文件名称
     * @throws Exception
     */
    public static void downloadByNIO2(String urlStr, String fileName, String saveDir) {
        File fileDir = new File(saveDir);
        // 判断文件夹是否存在
        if (!fileDir.exists()) {
            // 如果文件夹不存在,则创建新的的文件夹
            fileDir.mkdirs();
        }
        InputStream ins = null;
        try {
            // 创建SSLContext
            SSLContext sslContext = SSLContext.getInstance("SSL");
            TrustManager[] tm = {new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] var1, String var2) throws CertificateException {
                    // TODO Auto-generated method stub
                }
 
                @Override
                public void checkServerTrusted(X509Certificate[] var1, String var2) throws CertificateException {
                    // TODO Auto-generated method stub
                }
 
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    // TODO Auto-generated method stub
                    return null;
                }
            }};
            // 初始化
            sslContext.init(null, tm, new java.security.SecureRandom());
            // 获取SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();
            // url对象
            URL url = new URL(urlStr);
            // 打开连接
            URLConnection conn = url.openConnection();
            if (conn instanceof HttpsURLConnection) {
                HttpsURLConnection connection = (HttpsURLConnection)conn;
                connection.setHostnameVerifier(new HostnameVerifier() {
                    public boolean verify(String var1, SSLSession var2) {
                        return true;
                    }
                });
                // 设置当前实例使用的SSLSoctetFactory
                connection.setSSLSocketFactory(ssf);
            }
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            conn.connect();
            ins = conn.getInputStream();
            Path target = Paths.get(saveDir, fileName);
            Files.createDirectories(target.getParent());
            Files.copy(ins, target, StandardCopyOption.REPLACE_EXISTING);
        } catch (Exception e) {
             e.printStackTrace();
        } finally {
            try {
                ins.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

posted @ 2021-02-03 13:58  今天早点睡  阅读(308)  评论(0编辑  收藏  举报