|
@@ -156,6 +156,61 @@ public class FileUtils {
|
|
|
log.info("downloaded successfully [{}] to [{}]", fileUrl, filePath);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public static void downloadLPLD(String fileUrl, String filePath, boolean useUa, boolean useProxy, Map<String, String> proxyInfo) throws Exception {
|
|
|
+ log.info("begin download [{}] to [{}] useUa [{}] useProxy [{}] proxyInfo[{}]", fileUrl, filePath, useUa, useProxy, proxyInfo);
|
|
|
+ URL url = new URL(fileUrl);
|
|
|
+ HttpURLConnection conn;
|
|
|
+ if (useProxy) {
|
|
|
+ String proxyUrl = proxyInfo.getOrDefault("url", "");
|
|
|
+ int port = Integer.parseInt(proxyInfo.getOrDefault("port", "0"));
|
|
|
+ String username = proxyInfo.getOrDefault("username", "");
|
|
|
+ String password = proxyInfo.getOrDefault("password", "");
|
|
|
+ // 创建代理服务的地址和端口
|
|
|
+ Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUrl, port));
|
|
|
+ Authenticator authenticator = new Authenticator() {
|
|
|
+ @Override
|
|
|
+ public PasswordAuthentication getPasswordAuthentication() {
|
|
|
+ return (new PasswordAuthentication(username, password.toCharArray()));
|
|
|
+ }
|
|
|
+ };
|
|
|
+ Authenticator.setDefault(authenticator);
|
|
|
+ conn = (HttpURLConnection) url.openConnection(proxy);
|
|
|
+ } else {
|
|
|
+ conn = (HttpURLConnection) url.openConnection();
|
|
|
+ }
|
|
|
+ // 设置请求头, 添加了防盗链 referer
|
|
|
+ conn.setRequestProperty("Accept", "*/*");
|
|
|
+ conn.setRequestProperty("Accept-Encoding", "identity;q=1, *;q=0");
|
|
|
+ conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
|
|
|
+ conn.setRequestProperty("Range", "bytes=0-");
|
|
|
+
|
|
|
+// conn.setRequestProperty("Host", "mpvideo.qpic.cn");
|
|
|
+// conn.setRequestProperty("Origin", "https://mp.weixin.qq.com");
|
|
|
+// conn.setRequestProperty("Referer", "https://mp.weixin.qq.com/");
|
|
|
+
|
|
|
+ if (useUa) {
|
|
|
+ conn.setRequestProperty("User-Agent", FakeUserAgent.getRandomUserAgent());
|
|
|
+ }
|
|
|
+ if (conn.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN) {
|
|
|
+ throw new CommonException(ExceptionEnum.URL_FORBIDDEN);
|
|
|
+ }
|
|
|
+ conn.setConnectTimeout(5000);
|
|
|
+ conn.setReadTimeout(5000);
|
|
|
+ log.info("download file size is {} of url [{}]", formatFileSize(conn.getContentLength()), fileUrl);
|
|
|
+
|
|
|
+ InputStream inputStream = conn.getInputStream();
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(filePath);
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int len;
|
|
|
+ while ((len = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ inputStream.close();
|
|
|
+ outputStream.close();
|
|
|
+ log.info("downloaded successfully [{}] to [{}]", fileUrl, filePath);
|
|
|
+ }
|
|
|
+
|
|
|
public static void downloadDY(String fileUrl, String filePath, boolean useUa, boolean useProxy, Map<String, String> proxyInfo) throws Exception {
|
|
|
log.info("begin download [{}] to [{}] useUa [{}] useProxy [{}] proxyInfo[{}]", fileUrl, filePath, useUa, useProxy, proxyInfo);
|
|
|
URL url = new URL(fileUrl);
|