import javax.net.ssl.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class ImageSpider {

	public static InputStream getUrlInputStream(String url) {
		InputStream in = null;
		try {
			URL u = new URL(url);
			if (u.getProtocol().toLowerCase().equals("https")) {
				HttpsURLConnection https = (HttpsURLConnection) u.openConnection();
				https.setSSLSocketFactory(createSSL());
				https.setConnectTimeout(5000);
				https.setReadTimeout(5000);
				https.setDoOutput(true);
				https.setRequestMethod("GET");
				https.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0");
				https.connect();
				System.out.println(https.getResponseCode() + " " + https.getResponseMessage());
				in = https.getInputStream();
			} else {
				HttpURLConnection conn = (HttpURLConnection) u.openConnection();
				conn.connect();
				System.out.println(conn.getResponseCode() + " " + conn.getResponseMessage());
				in = conn.getInputStream();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return in;
	}

	public static void download(InputStream in, File dest) {
		OutputStream os = null;
		try {
			os = new FileOutputStream(dest);
			byte[] buff = new byte[1024];
			while(true){
				int readed = in.read(buff);//读取内容长度
				if(readed == -1){
					break;
				}
				byte[] temp = new byte[readed];
				System.arraycopy(buff, 0, temp, 0, readed);//内容复制
				os.write(temp);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private static SSLSocketFactory createSSL() throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, KeyStoreException, CertificateException, FileNotFoundException, IOException{
		TrustManager[] tm =new TrustManager[]{
				myTrustManager
		};
		SSLContext sslContext = SSLContext.getInstance("TLS");

		sslContext.init(null, tm, null);
		SSLSocketFactory ssf = sslContext.getSocketFactory();
		HttpsURLConnection.setDefaultHostnameVerifier((arg0, arg1) -> true);
		HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
		return ssf;
	}
	private static final TrustManager myTrustManager = new X509TrustManager()
	{
		@Override
		public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

		@Override
		public void checkServerTrusted(X509Certificate[] arg0, String arg1){}

		@Override
		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}
	};

	// 示例
	public static void main(String[] args) {
		download(getUrlInputStream("https://cdn.lixingyong.com/2022/03/07/22.jpg"), new File("D:\\a.jpg"));
	}
}
上一篇 下一篇