reghao 3 jaren geleden
bovenliggende
commit
30311c7ae8

+ 46 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/converter/ByteHex.java

@@ -0,0 +1,46 @@
+package cn.reghao.jutil.jdk.converter;
+
+/**
+ * 字节数组和十六进制字符串转换
+ *
+ * @author reghao
+ * @date 2022-04-27 16:38:34
+ */
+public class ByteHex {
+    private static final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+
+    /**
+     * 字节数组转换为十六进制字符串
+     *
+     * @param
+     * @return
+     * @date 2021-11-26 下午5:25
+     */
+    public static String bytes2Hex(byte[] bytes) {
+        StringBuilder hexStr = new StringBuilder();
+        for (byte b : bytes) {
+            int num = b < 0 ? 256 + b : b;
+            hexStr.append(HEX[num/16]).append(HEX[num%16]);
+        }
+
+        return hexStr.toString();
+    }
+
+    /**
+     * 十六进制字符串转换为字节数组
+     *
+     * @param
+     * @return
+     * @date 2021-11-26 下午5:25
+     */
+    public static byte[] hex2Bytes(String hex) {
+        int len = hex.length();
+        byte[] bytes = new byte[len/2];
+        for (int i = 0; i < len/2; i++) {
+            String tmp = hex.substring(i*2, i*2+2);
+            bytes[i] = (byte) Integer.parseInt(tmp, 16);
+        }
+
+        return bytes;
+    }
+}

+ 5 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/http/WebClient.java

@@ -1,5 +1,6 @@
 package cn.reghao.jutil.jdk.http;
 
+import java.io.IOException;
 import java.net.URI;
 import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
@@ -65,4 +66,8 @@ public class WebClient implements WebRequest {
     public WebResponse upload(String url, UploadParam uploadParam) {
         return null;
     }
+
+    @Override
+    public void download(String url, String dir) throws IOException {
+    }
 }

+ 2 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/http/WebRequest.java

@@ -1,5 +1,6 @@
 package cn.reghao.jutil.jdk.http;
 
+import java.io.IOException;
 import java.util.Map;
 
 /**
@@ -14,4 +15,5 @@ public interface WebRequest {
     WebResponse postFormData(String url, Map<String, String> formData);
     WebResponse postJson(String url, String json);
     WebResponse upload(String url, UploadParam uploadParam);
+    void download(String url, String filePath) throws IOException;
 }

+ 24 - 1
tool/src/main/java/cn/reghao/jutil/tool/http/DefaultWebRequest.java

@@ -3,6 +3,8 @@ package cn.reghao.jutil.tool.http;
 import cn.reghao.jutil.jdk.http.UploadParam;
 import cn.reghao.jutil.jdk.http.WebRequest;
 import cn.reghao.jutil.jdk.http.WebResponse;
+import cn.reghao.jutil.jdk.http.util.UserAgents;
+import org.apache.http.HttpEntity;
 import org.apache.http.NameValuePair;
 import org.apache.http.StatusLine;
 import org.apache.http.client.entity.UrlEncodedFormEntity;
@@ -11,11 +13,12 @@ import org.apache.http.entity.StringEntity;
 import org.apache.http.entity.mime.HttpMultipartMode;
 import org.apache.http.entity.mime.MultipartEntityBuilder;
 import org.apache.http.entity.mime.content.FileBody;
-import org.apache.http.entity.mime.content.InputStreamBody;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.EntityUtils;
 
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.util.*;
 
@@ -108,6 +111,7 @@ public class DefaultWebRequest extends BaseWebRequest implements WebRequest {
     }
 
     private WebResponse execRequest(HttpRequestBase request) {
+        request.setHeader("User-Agent", UserAgents.getDesktopAgent());
         try (CloseableHttpResponse response = client.execute(request)) {
             StatusLine statusLine = response.getStatusLine();
             int statusCode = statusLine.getStatusCode();
@@ -119,4 +123,23 @@ public class DefaultWebRequest extends BaseWebRequest implements WebRequest {
             return new WebResponse(600, e.getMessage());
         }
     }
+
+    public void download(String url, String filePath) throws IOException {
+        HttpGet get = new HttpGet(url);
+        get.setHeader("User-Agent", UserAgents.getDesktopAgent());
+        long start = System.currentTimeMillis();
+        try (CloseableHttpResponse response = client.execute(get)) {
+            int statusCode = response.getStatusLine().getStatusCode();
+            if (statusCode == 200) {
+                HttpEntity httpEntity = response.getEntity();
+                String contentType = httpEntity.getContentType().getValue();
+                File file = new File(filePath);
+                FileOutputStream fout = new FileOutputStream(file);
+                // 持续写到本地文件,直到服务器没有数据
+                httpEntity.writeTo(fout);
+            }
+        } catch (IOException e) {
+            throw e;
+        }
+    }
 }

+ 5 - 5
tool/src/main/java/cn/reghao/jutil/tool/http/JdkCrawlRequest.java

@@ -1,6 +1,7 @@
 package cn.reghao.jutil.tool.http;
 
 import cn.reghao.jutil.jdk.http.WebResponse;
+import cn.reghao.jutil.jdk.http.util.UrlFormatter;
 import cn.reghao.jutil.jdk.http.util.UserAgents;
 import org.apache.commons.io.FileUtils;
 
@@ -63,21 +64,20 @@ public class JdkCrawlRequest {
         }
     }
 
-    public boolean download(String url, File file) {
+    public void download(String url, String dir) throws IOException, InterruptedException {
         HttpRequest.Builder builder = HttpRequest.newBuilder()
                 .uri(URI.create(url))
                 .timeout(Duration.ofSeconds(30))
                 .GET();
         builder.setHeader("User-Agent", UserAgents.getDesktopAgent());
-
         try {
             HttpResponse<InputStream> in = client.send(builder.build(), HttpResponse.BodyHandlers.ofInputStream());
+            String filename = UrlFormatter.getFilename(url);
+            File file = new File(dir + File.separator + filename);
             saveFile(in.body(), file);
-            return true;
         } catch (Exception e) {
-            log.info(MessageFormat.format("{0} 下载失败 -> {1}", url, e.getMessage()));
+            throw e;
         }
-        return false;
     }
 
     private void saveFile(InputStream in, File file) throws IOException {