kongdeqiang
2024-04-07 8e51195319ea210e7ba06aabdabc40d64df14b08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.boying.util;
 
import com.boying.common.SystemConfigProperties;
import com.boying.entity.FileInfo;
import com.boying.entity.OrderRecord;
import com.boying.service.FileInfoService;
import com.boying.service.OrderRecordService;
import lombok.AllArgsConstructor;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;
 
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;
import java.util.UUID;
 
@Component
@AllArgsConstructor
public class FileUtil {
    private static SystemConfigProperties systemConfigProperties;
    private static FileInfoService fileInfoService;
    private static OrderRecordService orderRecordService;
 
    public static String saveFile(MultipartFile multipartFile){
        String filePath = systemConfigProperties.getFilePath();
        File file = null;
        if(multipartFile.getSize() <= 0){
            return null;
        }else {
            //文件原名称
            String originalFilename = multipartFile.getOriginalFilename();
            //文件格式
            String substring = Objects.requireNonNull(originalFilename).substring(originalFilename.lastIndexOf("."));
            //文件id
            String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
            //文件路径
            file=new File(filePath+File.separator+uuid+substring);
            String absPath = null;
            try {
                absPath = file.getCanonicalPath();
                String substring1 = absPath.substring(0, absPath.lastIndexOf(File.separator));
                File file1 = new File(substring1);
                if(!file1.exists()){
                    file1.mkdirs();
                }
                //写入
                InputStream inputStream = multipartFile.getInputStream();
                inputStreamToFile(inputStream,file);
                inputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            return absPath;
        }
 
    }
 
    public static void inputStreamToFile(InputStream inputStream,File file){
        try {
           OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inputStream.read(buffer,0,8192)) != -1){
                os.write(buffer,0,bytesRead);
            }
            os.close();
            inputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
    public static FileInfo generateBase64StringToFile(String fileString,int t,int type){
        String lastname= null;
        //文件id
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        if(t == 1){
            lastname=".png";
        }else {
            lastname=".pdf";
        }
        String filePath = "D:\\FFproject\\file\\";
        if(fileString == null){
            return null;
        }else {
            try {
                byte[] b = Base64.decodeBase64(fileString);
                for (int i = 0; i < b.length; ++i) {
                    if(b[i]<0){
                        b[i]+=256;
                    }
                }
                OutputStream out = new FileOutputStream(filePath+uuid+lastname);
                out.write(b);
                FileInfo fileInfo = new FileInfo();
                fileInfo.setFileBaseDir(filePath);
                fileInfo.setFileName(uuid);
                fileInfo.setFileLast(lastname);
                fileInfo.setType(type);
                out.flush();
                out.close();
                return fileInfo;
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
 
        }
    }
 
    public static String netSourceToBase64(String srcUrl,String requestMethod) {
        ByteArrayOutputStream outPut = new ByteArrayOutputStream();
        byte[] data = new byte[1024 * 8];
        try {
            // 创建URL
            URL url = new URL(srcUrl);
            // 创建链接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod(requestMethod);
            conn.setConnectTimeout(20000);
 
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                //连接失败/链接失效/文件不存在
                return null;
            }
            InputStream inStream = conn.getInputStream();
            int len = -1;
            while (-1 != (len = inStream.read(data))) {
                outPut.write(data, 0, len);
            }
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(outPut.toByteArray());
    }
 
    public static void main(String[] args) {
        String get = netSourceToBase64("http://36.134.129.218:9999/admin/sys-file/platform/20230815083801.png", "GET");
        System.out.println(get);
    }
 
}