All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.litongjava.tio.utils.http.HttpDownloadUtils Maven / Gradle / Ivy

There is a newer version: 3.7.3.v202400213-RELEASE
Show newest version
package com.litongjava.tio.utils.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class HttpDownloadUtils {

  static {
    // 设置一个信任所有证书的 TrustManager
    X509TrustManager x509TrustManager = new X509TrustManager() {
      public X509Certificate[] getAcceptedIssuers() {
        return null;
      }

      public void checkClientTrusted(X509Certificate[] certs, String authType) {
      }

      public void checkServerTrusted(X509Certificate[] certs, String authType) {
      }
    };

    TrustManager[] trustAllCerts = new TrustManager[] { x509TrustManager };

    // 设置全局的 SSLContext 以信任所有的证书
    try {
      SSLContext sc = SSLContext.getInstance("TLS");
      sc.init(null, trustAllCerts, new SecureRandom());
      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
      HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static ByteArrayOutputStream download(String remoteURL, Map headers) {
    URL httpURL;
    try {
      httpURL = new URL(remoteURL);
    } catch (MalformedURLException e) {
      throw new RuntimeException(e);
    }

    HttpURLConnection httpConnection;
    try {
      httpConnection = (HttpURLConnection) httpURL.openConnection();
      if (httpConnection instanceof HttpsURLConnection) {
        ((HttpsURLConnection) httpConnection).setSSLSocketFactory(HttpsURLConnection.getDefaultSSLSocketFactory());
        ((HttpsURLConnection) httpConnection).setHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier());
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    // 设置 HTTP 头
    if (headers != null && headers.size() > 0) {
      Set> entrySet = headers.entrySet();
      for (Entry entry : entrySet) {
        httpConnection.setRequestProperty(entry.getKey(), entry.getValue());
      }
    }

    httpConnection.setDoInput(true);
    httpConnection.setDoOutput(true);

    try (InputStream inputStream = httpConnection.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {

      int length;
      byte[] buffer = new byte[1024 * 1024 * 2]; // 一次读取2M

      while ((length = inputStream.read(buffer)) != -1) {
        byteArrayOutputStream.write(buffer, 0, length); // 将文件保存到内存流
      }

      return byteArrayOutputStream;
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy