Parcourir la source

Merge branch 'dev' of depot-qinghai/depot-timer-task into master

gdd il y a 5 mois
Parent
commit
0c24bee526

+ 6 - 0
pom.xml

@@ -19,6 +19,7 @@
19 19
         <java.version>1.8</java.version>
20 20
         <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
21 21
         <skipTests>true</skipTests>
22
+        <hutool-all.version>5.8.29</hutool-all.version>
22 23
     </properties>
23 24
     <dependencies>
24 25
         <dependency>
@@ -46,6 +47,11 @@
46 47
             <artifactId>spring-boot-starter-test</artifactId>
47 48
             <scope>test</scope>
48 49
         </dependency>
50
+        <dependency>
51
+            <groupId>cn.hutool</groupId>
52
+            <artifactId>hutool-all</artifactId>
53
+            <version>${hutool-all.version}</version>
54
+        </dependency>
49 55
     </dependencies>
50 56
     <dependencyManagement>
51 57
         <dependencies>

+ 39 - 0
src/main/java/com/chinaitop/util/NetFileUtil.java

@@ -0,0 +1,39 @@
1
+package com.chinaitop.util;
2
+
3
+import cn.hutool.http.HttpUtil;
4
+
5
+import java.util.Base64;
6
+
7
+/**
8
+ * 网络文件工具
9
+ *
10
+ * @author ybw
11
+ * @version V1.0
12
+ * @className FileUtil
13
+ * @date 2024/7/19
14
+ **/
15
+public class NetFileUtil {
16
+
17
+    /**
18
+     * 网络文件转文件流
19
+     */
20
+    public static byte[] urlToStream(String url) {
21
+        return HttpUtil.downloadBytes(url);
22
+    }
23
+
24
+
25
+    /**
26
+     * 获取Base64编码的字符串
27
+     *
28
+     * @param url 网络文件地址
29
+     * @methodName: getBase64Content
30
+     * @return: java.lang.String 网络文件地址
31
+     * @author: ybw
32
+     * @date: 2024/7/19
33
+     **/
34
+    public static String getBase64Content(String url) {
35
+        byte[] bytes = urlToStream(url);
36
+        // 将文件内容转换为Base64字符串
37
+        return Base64.getEncoder().encodeToString(bytes);
38
+    }
39
+}

+ 67 - 0
src/test/java/com/chinaitop/file/NetFileTest.java

@@ -0,0 +1,67 @@
1
+package com.chinaitop.file;
2
+
3
+import cn.hutool.core.io.FileUtil;
4
+import com.chinaitop.util.NetFileUtil;
5
+import lombok.extern.slf4j.Slf4j;
6
+import org.junit.jupiter.api.BeforeEach;
7
+import org.junit.jupiter.api.Test;
8
+
9
+import java.io.File;
10
+import java.util.Base64;
11
+
12
+/**
13
+ * 下载网络文件测试
14
+ *
15
+ * @author ybw
16
+ * @version V1.0
17
+ * @className FileTest
18
+ * @date 2024/7/19
19
+ **/
20
+@Slf4j
21
+public class NetFileTest {
22
+
23
+    /**
24
+     * 网络文件
25
+     *
26
+     * @author: ybw
27
+     * @date: 2024/7/19
28
+     **/
29
+    private String url;
30
+
31
+    @BeforeEach
32
+    public void init() {
33
+        url = "http://172.16.0.36:15772/province-qinghai/2024/7/19/1814134555203710977.jpg";
34
+    }
35
+
36
+    /**
37
+     * 网络文件转文件流
38
+     */
39
+    @Test
40
+    public void fileToStream() {
41
+        byte[] bytes = NetFileUtil.urlToStream(url);
42
+        log.info("bytes:{}", bytes);
43
+    }
44
+
45
+    /**
46
+     * 网络文件转为Base64
47
+     */
48
+    @Test
49
+    public void fileToBase64() {
50
+        String base64Content = NetFileUtil.getBase64Content(url);
51
+        // 输出Base64编码的字符串
52
+        log.info("base64:{}", base64Content);
53
+    }
54
+
55
+
56
+    /**
57
+     * Base64转文件
58
+     */
59
+    @Test
60
+    public void base64ToFile() {
61
+        File file = new File("D:\\1\\1.jpg");
62
+        String base64Content = NetFileUtil.getBase64Content(url);
63
+        FileUtil.writeBytes(Base64.getDecoder().decode(base64Content), file);
64
+    }
65
+
66
+
67
+}