kongdeqiang
2023-06-08 3fba1d84220268d871c3c28e0e25f6eab3526f46
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
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 java.io.*;
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;
            }
 
        }
    }
 
}