CZT
2023-11-27 c206acfaedc69c390fb67daa81bc686f58a212ef
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.ld.igds.basic.manager;
 
import com.ld.igds.constant.RespCodeEnum;
import com.ld.igds.data.PageResponse;
import com.ld.igds.file.CoreFileService;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.FilesUtil;
 
import lombok.extern.slf4j.Slf4j;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
 
/**
 * 文件上传管理类
 *
 * @author
 */
@Slf4j
@Component
public class FileManager {
 
    @Autowired
    private CoreFileService fileService;
    @Resource
    private FilesUtil filesUtil;
 
 
    /**
     * 出入库的附件上次,参数是车牌号
     *
     * @param file
     * @return
     * @throws IOException
     */
    public PageResponse<String> upLoadInoutImg(MultipartFile file, String plateNum)
            throws IOException {
 
        log.debug("上传出入库的附件……");
 
        // 获取默认编码的字节数组
        byte[] fileByte = file.getBytes();
 
        // 获取文件的源文件名称
        String oldFileName = file.getOriginalFilename();
 
        // 获取文件保存路径
        String filePath = fileService.getInoutFilePath(new Date());
 
        // 获取新的ID
        String newFileName = ContextUtil.getUUID();
 
        // 文件后缀名
        String suffixName = oldFileName.substring(oldFileName.lastIndexOf("."));
 
        // 合成新的文件名
        if (StringUtils.isEmpty(plateNum)) {
            newFileName = newFileName + suffixName;
        } else {
            newFileName = plateNum + "_" + newFileName + suffixName;
        }
 
        // 文件上传
        boolean flag = uploadFile(fileByte, filePath, newFileName);
 
        // 判断文件上传是否成功,成功返回文件名
        if (flag) {
            return new PageResponse<String>(RespCodeEnum.CODE_0000.getCode(),
                    newFileName);
        } else {
            return new PageResponse<String>(RespCodeEnum.CODE_1111.getCode(),
                    "上传失败");
        }
    }
 
    /**
     * 文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    public PageResponse<String> upLoad(MultipartFile file, String bizTag)
            throws IOException {
 
        // 获取默认编码的字节数组
        byte[] fileByte = file.getBytes();
 
        // 获取文件的源文件名称
        String oldFileName = file.getOriginalFilename();
 
        // 获取文件保存路径
        String filePath = fileService.getInoutFilePath(new Date());
 
        // 获取新的ID
        String newFileName = ContextUtil.getUUID();
 
        // 文件后缀名
        String suffixName = oldFileName.substring(oldFileName.lastIndexOf("."));
 
        // 合成新的文件名
        if (StringUtils.isEmpty(bizTag)) {
            newFileName = newFileName + suffixName;
        } else {
            newFileName = bizTag + "_" + newFileName + suffixName;
        }
        // 文件上传
        boolean flag = uploadFile(fileByte, filePath, newFileName);
 
        // 判断文件上传是否成功,成功返回文件名
        if (flag) {
            return new PageResponse<String>(RespCodeEnum.CODE_0000.getCode(),
                    newFileName);
        } else {
            return new PageResponse<String>(RespCodeEnum.CODE_1111.getCode(),
                    "上传失败");
        }
    }
 
    /**
     * 文件流上传文件
     *
     * @param file
     * @param filePath
     * @param fileName
     * @return
     */
    private static boolean uploadFile(byte[] file, String filePath, String fileName) throws IOException {
        // 默认文件上传成功
        boolean flag = true;
        // new一个文件对象实例
        File targetFile = new File(filePath);
        // 如果当前文件目录不存在就自动创建该文件或者目录
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        // 创建文件流
        FileOutputStream fileOutputStream = new FileOutputStream(filePath + fileName);
        try {
            fileOutputStream.write(file);
        } catch (FileNotFoundException e) {
            flag = false;
        } catch (IOException ioException) {
            flag = false;
        } finally {
            fileOutputStream.flush();
            fileOutputStream.close();
        }
        return flag;
    }
 
    public File getTempFilePath(String fileName, String companyId) {
        String path = filesUtil.getTempPath(companyId) + "/" + fileName;
        return new File(path);
    }
    
    public String getSnapFilePath(String fileName, String dateStr) {
       return  fileService.getSnapFilePath(dateStr) + "/" + fileName;
    }
 
    public String getFailImg() throws IOException {
        
        String failImg = "static/images/img-fail.jpg";
        
        ClassPathResource readFile = new ClassPathResource(failImg);
        
        return readFile.getFile().getAbsolutePath();
    }
}