|
@@ -8,13 +8,19 @@
|
|
package cn.com.qmth.examcloud.core.print.common.utils;
|
|
package cn.com.qmth.examcloud.core.print.common.utils;
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
+import org.apache.http.ssl.SSLContextBuilder;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
+import javax.net.ssl.HttpsURLConnection;
|
|
|
|
+import javax.net.ssl.SSLContext;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
-import java.net.MalformedURLException;
|
|
|
|
|
|
+import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.Charset;
|
|
|
|
+import java.security.KeyManagementException;
|
|
|
|
+import java.security.KeyStoreException;
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
import java.util.UUID;
|
|
import java.util.UUID;
|
|
@@ -173,14 +179,66 @@ public class FileUtils {
|
|
|
|
|
|
public static void saveURLToFile(String link, File file) {
|
|
public static void saveURLToFile(String link, File file) {
|
|
if (link == null || file == null) {
|
|
if (link == null || file == null) {
|
|
- // ignore
|
|
|
|
|
|
+ log.warn("link or file must be not null.");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if (link.startsWith("https")) {
|
|
|
|
+ saveHttpsURLToFile(link, file);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
try {
|
|
try {
|
|
URL url = new URL(link);
|
|
URL url = new URL(link);
|
|
org.apache.commons.io.FileUtils.copyURLToFile(url, file);
|
|
org.apache.commons.io.FileUtils.copyURLToFile(url, file);
|
|
- } catch (MalformedURLException e) {
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void saveHttpsURLToFile(String link, File file) {
|
|
|
|
+ SSLContext sslContext = null;
|
|
|
|
+ try {
|
|
|
|
+ //System.setProperty ("jsse.enableSNIExtension", "false");
|
|
|
|
+ sslContext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build();
|
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ } catch (KeyStoreException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ } catch (KeyManagementException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (sslContext == null) {
|
|
|
|
+ log.warn("http sslContext must be not null.");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ HttpsURLConnection connection;
|
|
|
|
+ try {
|
|
|
|
+ URL url = new URL(link);
|
|
|
|
+ HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
|
|
|
|
+ connection = (HttpsURLConnection) url.openConnection();
|
|
|
|
+ connection.setConnectTimeout(15000);
|
|
|
|
+ connection.setReadTimeout(15000);
|
|
|
|
+
|
|
|
|
+ if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
|
|
|
|
+ log.warn("http connection response fail." + connection.getResponseMessage());
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
log.error(e.getMessage(), e);
|
|
log.error(e.getMessage(), e);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //响应成功
|
|
|
|
+ try (DataInputStream in = new DataInputStream(connection.getInputStream());
|
|
|
|
+ DataOutputStream out = new DataOutputStream(new FileOutputStream(file));) {
|
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
|
+ int len;
|
|
|
|
+ while ((len = in.read(bytes)) > 0) {
|
|
|
|
+ out.write(bytes, 0, len);
|
|
|
|
+ }
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
log.error(e.getMessage(), e);
|
|
log.error(e.getMessage(), e);
|
|
}
|
|
}
|