Http Download

Http / Https 下载文件,startWith不能验证https,测试地址:https://storage.googleapis.com/golang/go1.7.3.windows-amd64.msi

java 复制代码
private static final Logger logger = Logger.
		getLogger(MethodHandles.lookup().lookupClass());
	
	private static void sunNetSsl() {
		System.setProperty("java.protocol.handler.pkgs",
			"com.sun.net.ssl.internal.www.protocol");
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
	}
	
	private static void javaxNetSsl(){
		TrustManager[] trustAllCertArr = new TrustManager[]{
			new X509TrustManager() {
				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}
				public void checkServerTrusted(X509Certificate[] 
					certArr,String authType) throws CertificateException {}
				public void checkClientTrusted(X509Certificate[] 
					certArr,String authType) throws CertificateException {}
		}};
		//activate trust manager
		try {
			SSLContext sc = SSLContext.getInstance("SSL");
			sc.init(null,trustAllCertArr,new SecureRandom());
			HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
		} catch (NoSuchAlgorithmException | KeyManagementException e) {
			e.printStackTrace();
		}
	}
	
	public static void download(String downloadUrl,String fileName,boolean isHttps) throws IOException{
		if (isHttps) {
			logger.info(isHttps);
			javaxNetSsl();
			//sunNetSsl();
		}
		URL url = new URL(downloadUrl);
		try(ReadableByteChannel rbc = Channels.newChannel(url.openStream());
			FileOutputStream fos = new FileOutputStream(fileName)){
			fos.getChannel().transferFrom(rbc,0,Long.MAX_VALUE);
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
相关推荐
Christal_pyy18 分钟前
树莓派4基于Debian GNU/Linux 12 (Bookworm)添加多个静态ipv4网络
linux·网络·debian
csbDD1 小时前
2025年网络安全(黑客技术)三个月自学手册
linux·网络·python·安全·web安全
s_fox_2 小时前
nginx ngx_http_module(7) 指令详解
运维·nginx·http
荔枝荷包蛋6662 小时前
【Linux】HTTP:Cookie 和 Session 详解
网络·网络协议·http
EasyNVR2 小时前
EasyRTC智能硬件:实时畅联、沉浸互动、消音护航
运维·服务器·网络·安全·音视频·webrtc·p2p
CAir22 小时前
HTTP SSE 实现
http·libhv·sse
劉煥平CHN2 小时前
RabbitMQ的脑裂(网络分区)问题
网络·分布式·rabbitmq
阿湯哥3 小时前
越权漏洞及其修复方法
网络·安全·web安全
遥遥远方 近在咫尺3 小时前
HTTPS原理
网络协议·https
s_fox_3 小时前
nginx ngx_http_module(9) 指令详解
运维·nginx·http