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();
		}
	}
	
相关推荐
❥ღ Komo·1 天前
Ansible Playbook入门指南:核心语法与实战
网络
雪芽蓝域zzs1 天前
uni-app 将 base64 图片编码转为 Blob 本地文件路径
网络协议·udp·uni-app
Σdoughty1 天前
DNS主从服务
网络
CS_浮鱼1 天前
【Linux】进程控制
linux·运维·网络
三川6981 天前
1. 网络编程基础
开发语言·网络
Eloudy1 天前
节点内 cuda GPU 之间 P2P IPC 通信的硬件机制参考
网络协议·p2p
atsec1 天前
atsec完成Newland NPT的P2PE PA评估
服务器·网络协议·npt·p2pe
Guheyunyi1 天前
安全风险监测系统核心技术
运维·网络·人工智能·安全
weixin_307779131 天前
Amazon VPC中Web应用无法连接数据库的安全组配置问题分析与修复
网络·数据库·安全·云计算·aws
奥吼吼~~1 天前
标准输入输出stdio和JSON-RPC
网络协议·rpc·json