|
|
@@ -2,9 +2,8 @@ package com.railway.common.utils.http;
|
|
|
|
|
|
import com.railway.common.constant.Constants;
|
|
|
import com.railway.common.utils.StringUtils;
|
|
|
-import java.io.BufferedInputStream;
|
|
|
-import java.io.BufferedOutputStream;
|
|
|
import java.io.BufferedReader;
|
|
|
+import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
@@ -91,8 +90,8 @@ public class HttpUtils {
|
|
|
* @param url 发送请求的 URL
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
*/
|
|
|
- public static byte [] sendGetStream(String url) {
|
|
|
- InputStream in = sendGetStream(url, null);
|
|
|
+ public static byte [] sendGetStreamByte(String url) {
|
|
|
+ InputStream in = sendGetStream(url);
|
|
|
if(null == in){
|
|
|
return new byte[0];
|
|
|
}
|
|
|
@@ -111,36 +110,41 @@ public class HttpUtils {
|
|
|
urlNameString = url + "?" + param;
|
|
|
}
|
|
|
log.info("sendGet - {}", urlNameString);
|
|
|
+ return sendGetStream(urlNameString);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 向指定 URL 发送GET方法的请求
|
|
|
+ *
|
|
|
+ * @param url 发送请求的 URL
|
|
|
+ * @return 所代表远程资源的响应结果
|
|
|
+ */
|
|
|
+ public static InputStream sendGetStream(String url) {
|
|
|
+ log.info("sendGet - {}", url);
|
|
|
try {
|
|
|
- URLConnection connection = getRedirectUrlConnection(urlNameString);
|
|
|
+ URLConnection connection = getRedirectUrlConnection(url);
|
|
|
connection.connect();
|
|
|
return connection.getInputStream();
|
|
|
- } catch (ConnectException e) {
|
|
|
- log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
|
|
|
- } catch (SocketTimeoutException e) {
|
|
|
- log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
|
} catch (IOException e) {
|
|
|
- log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
|
|
|
+ log.error("调用HttpUtils.sendGet IOException, url=" + url, e);
|
|
|
} catch (Exception e) {
|
|
|
- log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
|
|
|
+ log.error("调用HttpsUtil.sendGet Exception, url=" + url, e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public static void download(String url, String filePath) throws Exception {
|
|
|
- String urlNameString = getRedirectUrl(url);
|
|
|
- URL realUrl = new URL(urlNameString);
|
|
|
- log.info("download - {}", urlNameString);
|
|
|
- try(BufferedInputStream in = new BufferedInputStream(realUrl.openStream());
|
|
|
- BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filePath))){
|
|
|
- //缓冲字节数组
|
|
|
- byte[] data = new byte[2048];
|
|
|
- int length = in.read(data);
|
|
|
- while (length != -1) {
|
|
|
- out.write(data, 0, data.length);
|
|
|
- length = in.read(data);
|
|
|
- }
|
|
|
- }catch (IOException e){
|
|
|
+ public static void download(String url, String filePath) {
|
|
|
+ InputStream in = sendGetStream(url);
|
|
|
+ if(null == in){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.debug(" ++ {}", filePath);
|
|
|
+ byte[] byteArray = inputStream2ByteArray(in);
|
|
|
+ log.debug(" -- {}", url);
|
|
|
+ File file = new File(filePath);
|
|
|
+ try (FileOutputStream os = new FileOutputStream(file, true)){
|
|
|
+ os.write(byteArray, 0, byteArray.length);
|
|
|
+ } catch (IOException e) {
|
|
|
log.error("下载出错 : {}", url);
|
|
|
}
|
|
|
}
|
|
|
@@ -274,14 +278,17 @@ public class HttpUtils {
|
|
|
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
|
|
|
conn.setInstanceFollowRedirects(false);
|
|
|
conn.setConnectTimeout(5000);
|
|
|
+ conn.setRequestProperty("accept", "*/*");
|
|
|
+ conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
+ conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
String redirectUrl = conn.getHeaderField("Location");
|
|
|
if(null != redirectUrl){
|
|
|
URL realUrl = new URL(redirectUrl);
|
|
|
conn = (HttpURLConnection) realUrl.openConnection();
|
|
|
+ conn.setRequestProperty("accept", "*/*");
|
|
|
+ conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
+ conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
}
|
|
|
- conn.setRequestProperty("accept", "*/*");
|
|
|
- conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
- conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
return conn;
|
|
|
}
|
|
|
|
|
|
@@ -348,7 +355,7 @@ public class HttpUtils {
|
|
|
|
|
|
public static void test(){
|
|
|
String url = "http://gdsc.svstrong.com/profile/2021/12/15/2254d969-a04f-4a46-888f-2d0a406678b0.png";
|
|
|
- byte[] byteArray = HttpUtils.sendGetStream(url);
|
|
|
+ byte[] byteArray = HttpUtils.sendGetStreamByte(url);
|
|
|
log.info("byteArray.length - {}", byteArray.length);
|
|
|
}
|
|
|
}
|