|
@@ -2,7 +2,9 @@ package com.qmth.boot.tools.models;
|
|
|
|
|
|
import com.qmth.boot.tools.codec.CodecUtils;
|
|
import com.qmth.boot.tools.codec.CodecUtils;
|
|
import com.qmth.boot.tools.io.IOUtils;
|
|
import com.qmth.boot.tools.io.IOUtils;
|
|
|
|
+import com.qmth.boot.tools.ssl.AllAcceptSSLContext;
|
|
|
|
|
|
|
|
+import javax.net.ssl.HttpsURLConnection;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.Charset;
|
|
@@ -34,8 +36,25 @@ public class ByteArray {
|
|
* @param url
|
|
* @param url
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static ByteArray fromUrl(String url) throws IOException {
|
|
|
|
- return ByteArray.fromInputStream(new URL(url).openStream());
|
|
|
|
|
|
+ public static ByteArray fromUrl(String url) throws Exception {
|
|
|
|
+ URL urlObj = new URL(url);
|
|
|
|
+ InputStream ins = null;
|
|
|
|
+ if (urlObj.getProtocol().equalsIgnoreCase("http")) {
|
|
|
|
+ ins = new URL(url).openStream();
|
|
|
|
+ } else if (urlObj.getProtocol().equalsIgnoreCase("https")) {
|
|
|
|
+ HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection();
|
|
|
|
+ conn.setSSLSocketFactory(AllAcceptSSLContext.get().getSocketFactory());
|
|
|
|
+ conn.setHostnameVerifier((s, sslSession) -> true);
|
|
|
|
+ conn.setInstanceFollowRedirects(false);
|
|
|
|
+ ins = conn.getInputStream();
|
|
|
|
+ } else {
|
|
|
|
+ throw new RuntimeException("Invalid url protocol, should be http or https");
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ return ByteArray.fromInputStream(ins);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(ins);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|