文章正文
java 通过HTTPS URL文件
【文档】2020-04-23
简介java 通过HTTPS URL下载文件
类
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import org.ueditor.exception.UeditorException;
import org.ueditor.util.X509TrustUtiil;
import com.baidu.ueditor.define.MIMEType;
public class Dowload {
public static void downImg(String urlStr, String fileLocal) {
try {
// 截取图片url,去掉参数
if (urlStr.indexOf("?") > 0) {
urlStr = urlStr.substring(0, urlStr.indexOf("?"));
}
InputStream inStream = null;
String suffix = null;
if (urlStr.startsWith("https://")) {
SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
sslcontext.init(null, new TrustManager[] { new X509TrustUtiil() }, new java.security.SecureRandom());
URL url = new URL(urlStr);
HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() {
public boolean verify(String s, SSLSession sslsession) {
System.out.println("WARNING: Hostname is not matched for cert.");
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setConnectTimeout(6000);
connection.setReadTimeout(6000);
int code = connection.getResponseCode();
if (code != HttpURLConnection.HTTP_OK) {
throw new UeditorException("文件读取失败");
}
//
System.out.println("imgUrl: " + urlStr);
System.out.println("connectionType: " + connection.getContentType());
suffix = MIMEType.getSuffix(connection.getContentType());
inStream = connection.getInputStream();
} else {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
connection.setInstanceFollowRedirects(true);
connection.setUseCaches(true);
int code = connection.getResponseCode();
if (code != HttpURLConnection.HTTP_OK) {
throw new UeditorException("文件读取失败");
}
//
System.out.println("imgUrl: " + urlStr);
System.out.println("connectionType: " + connection.getContentType());
suffix = MIMEType.getSuffix(connection.getContentType());
inStream = connection.getInputStream();
}
// 读文件流
DataInputStream in = new DataInputStream(inStream);
DataOutputStream out = new DataOutputStream(new FileOutputStream(fileLocal));
byte[] buffer = new byte[2048];
int count = 0;
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
out.close();
in.close();
} catch (Exception e) {
System.out.println("download failed");
}
}
public static void main(String[] args) {
String urlStr = "http://redisbook.readthedocs.io/en/latest/_images/graphviz-58f7b1f1f52b28f59291d194555fc9f4b1462a4c.svg";
urlStr = "https://img0.tuicool.com/uUJVjqQ.jpg!web";
urlStr = "http://cdn.layui.com/upload/2018_1/6167784_1515637044884_75870.png";
urlStr = "http://redisbook.readthedocs.io/en/latest/_images/graphviz-58f7b1f1f52b28f59291d194555fc9f4b1462a4c.svg";
String fileLocal = "d:\test.png";
downImg(urlStr, fileLocal);
}
}工具类
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
public class X509TrustUtiil implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
}
打赏支持
感谢您的支持,加油!
打开微信扫码打赏,你说多少就多少
找书费时,联系客服快速获取!
在线客服8:30-22:30,若离线请留言!
获取教程,请联系在线客服!
在线客服8:30-22:30,若离线请留言!
热门阅读
-
生态学(第四版) 杨持 高等教育出版社 PDF 9787040595628 出版时间:2023年08月.pdf
生态学(第四版) 杨持 高等教育出版社 PDF 9787040595628 出版时间:2023年08月.pdf ...
-
史前的现代化 中国农业起源过程的文化生态考察 陈胜前著 北京:科学出版社 PDF 9787030397263 2013.pdf
史前的现代化 中国农业起源过程的文化生态考察 陈胜前著 北京:科学出版社 PDF 9787030397263 2013.pdf ...
-
股票大作手操盘术 融合时间和价格的利弗莫尔准则 (美)利弗莫尔著;丁圣元译 北京:人民邮电出版社 PDF 9787115292360 2012.pdf
股票大作手操盘术 融合时间和价格的利弗莫尔准则 (美)利弗莫尔著;丁圣元译 北京:人民邮电出版社 PDF 9787115292360 2012.pdf ...
-
期货市场技术分析——期(现)货市场、股票市场、外汇市场、利率 (美)约翰·墨菲 丁圣元译 地震出版社 PDF 9787548282228 出版时间:1994.pdf
期货市场技术分析——期(现)货市场、股票市场、外汇市场、利率 (美)约翰·墨菲 丁圣元译 地震出版社 PDF 9787548282228 出版时间:1994.pdf ...
-
证券业从业人员资格考试教材 证券市场基础知识 中国证券业协会编 北京:中国金融出版社 PDF 9787504963819 2012.pdf
证券业从业人员资格考试教材 证券市场基础知识 中国证券业协会编 北京:中国金融出版社 PDF 9787504963819 2012.pdf ...
-
近世代数基础 修订本 张禾瑞著 北京:高等教育出版社 PDF 7040012227 2005.pdf
近世代数基础 修订本 张禾瑞著 北京:高等教育出版社 PDF 7040012227 2005.pdf ...
-
广联达BIM算量大赛实训图集 朱溢镕、王全杰主编 化学工业出版社 PDF 9787122234353 出版时间:2015年06月.pdf
广联达BIM算量大赛实训图集 朱溢镕、王全杰主编 化学工业出版社 PDF 9787122234353 出版时间:2015年06月.pdf ...
-
中国蜻蜓大图鉴 下 张浩淼著 重庆:重庆大学出版社 PDF 9787568910378 出版时间:2019.pdf
中国蜻蜓大图鉴 下 张浩淼著 重庆:重庆大学出版社 PDF 9787568910378 出版时间:2019.pdf ...
-
懂老公的女人最幸福大全集 超值金版 静涛,袁钰编著 上海:立信会计出版社 PDF 9787542936912 2012.pdf
懂老公的女人最幸福大全集 超值金版 静涛,袁钰编著 上海:立信会计出版社 PDF 9787542936912 2012.pdf ...
-
炼金术 伟大的奥秘 (意)Andrea Aromatico原著;李晓桦译 上海:上海书店出版社 PDF 7806229884 2002.pdf
炼金术 伟大的奥秘 (意)Andrea Aromatico原著;李晓桦译 上海:上海书店出版社 PDF 7806229884 2002.pdf ...