|
@@ -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
|
+}
|