czt
9 天以前 db67639449287bcec461916a7dca6003ee5dd03c
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
package com.fzzy.sys.manager.file;
 
import com.fzzy.igds.constant.RespCodeEnum;
import com.fzzy.igds.data.PageResponse;
import com.fzzy.igds.domain.Dept;
import com.fzzy.igds.service.CoreDeptService;
import com.fzzy.igds.service.FileService;
import com.fzzy.igds.utils.ContextUtil;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
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;
 
/**
 * @Description 出入库设备相关功能
 * @Author CZT
 * @Date 2025/12/4 9:27
 */
@Slf4j
@Component("file.fileManager")
public class FileManager {
    @Resource
    private CoreDeptService coreDeptService;
    @Resource
    private FileService fileService;
 
    /**
     * 根据单位ID获取单位信息
     *
     * @param deptId
     * @return
     */
    public String getDeptFile(String deptId) {
        if (StringUtils.isEmpty(deptId)) {
            return null;
        }
        String imgBathPath = "未上传";
        Dept dept = coreDeptService.getDeptById(deptId);
 
        if (StringUtils.isNotEmpty(dept.getImgName())) {
            File imgFile = new File(dept.getImgName());
            if (imgFile.exists()) {
                imgBathPath = dept.getImgName();
            }
        }
        return imgBathPath;
    }
 
    /**
     * 出入库的附件上次,参数是车牌号
     *
     * @param file
     * @return
     * @throws IOException
     */
    public PageResponse<String> upLoadInoutHandleImg(MultipartFile file, String plateNum)
            throws IOException {
 
        log.debug("上传出入库的附件……");
 
        // 获取默认编码的字节数组
        byte[] fileByte = file.getBytes();
 
        // 获取文件的源文件名称
        String oldFileName = file.getOriginalFilename();
 
        // 获取文件保存路径
        String filePath = fileService.getFileSavePath("INOUT");
 
        // 获取新的ID
        String newFileName = ContextUtil.generateId();
 
        // 文件后缀名
        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
     * @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;
    }
}