zhitong.yu
7 天以前 7fc9c42987a13c1d8d2159591a5803e70518827f
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.hxzk.gps.controller.UploadFile;
 
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.hxzk.gps.controller.UploadFile.deo.FileUpLoad;
import com.hxzk.gps.controller.UploadFile.deo.ReturnMessageUpload;
 
import com.hxzk.gps.util.MessageUtils.MessageUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.UUID;
 
/*
 * 上传照片
 * @author YuZhiTong
 * @since 2025-05-19
 *
 * */
@RestController
@RequestMapping("/UploadFile")
public class UploadPic {
 
    @Value("${upload.dir-Person}")
    private String uploadDirPerson;
 
    @Value("${upload.dir-DepartMent}")
    private String uploadDirDepartMent;
 
    @Value("${upload.dir-IP}")
    private String uploadIp;
 
    @Value("${upload.dir-CompanyLogo}")
    private String uploadDirCompanyLogo;
 
    @Value("${upload.dir-Hksxt}")
    private String uploadDirHksxtIcon;
    @Value("${upload.dir-UserImg}")
    private String UserImg;
 
    /*
     * 公司Logo上传
     * */
    @SaCheckLogin
    @PostMapping("/uploadUserImg")
    public ReturnMessageUpload uploadCompanyLogo(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
        }
        try {
            // 生成唯一的文件名,避免文件名冲突
            String fileName = UUID.randomUUID().toString() + "." + getFileExtension(file.getOriginalFilename());
            // 构建文件保存路径
            Path filePath = Paths.get(UserImg, fileName);
            // 检查上传目录是否存在,如果不存在则创建
            File dir = new File(UserImg);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            // 将上传的文件保存到指定路径
            Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
            FileUpLoad fileUpLoad = new FileUpLoad();
            fileUpLoad.setFileUrl(uploadIp+"User/"+fileName);
            return new ReturnMessageUpload(200, MessageUtils.getMessage("SuccessPicture"), fileUpLoad);
        } catch (IOException e) {
            e.printStackTrace();
            return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
        }
    }
 
 
    /*
    * 终端照片上传
    * */
    @SaCheckLogin
    @PostMapping("/uploadPersonPic")
    public ReturnMessageUpload uploadPersonPic(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
        }
        try {
            // 生成唯一的文件名,避免文件名冲突
            String fileName = UUID.randomUUID().toString() + "." + getFileExtension(file.getOriginalFilename());
            // 构建文件保存路径
            Path filePath = Paths.get(uploadDirPerson, fileName);
            // 检查上传目录是否存在,如果不存在则创建
            File dir = new File(uploadDirPerson);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            // 将上传的文件保存到指定路径
            Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
            FileUpLoad fileUpLoad = new FileUpLoad();
            fileUpLoad.setFileUrl(uploadIp+"Person/"+fileName);
            return new ReturnMessageUpload(200, MessageUtils.getMessage("SuccessPicture"), fileUpLoad);
        } catch (IOException e) {
            e.printStackTrace();
            return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
        }
    }
 
 
 
    /*
     * 部门图标上传
     * */
    @SaCheckLogin
    @PostMapping("/uploadDepartMentIconPic")
    public ReturnMessageUpload uploadDepartMentIconPic(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
        }
        try {
            // 生成唯一的文件名,避免文件名冲突
            String fileName = UUID.randomUUID().toString() + "." + getFileExtension(file.getOriginalFilename());
            // 构建文件保存路径
            Path filePath = Paths.get(uploadDirDepartMent, fileName);
            // 检查上传目录是否存在,如果不存在则创建
            File dir = new File(uploadDirDepartMent);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            // 将上传的文件保存到指定路径
            Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
            FileUpLoad fileUpLoad = new FileUpLoad();
            fileUpLoad.setFileUrl(uploadIp+"DepartMent/"+fileName);
            return new ReturnMessageUpload(200, MessageUtils.getMessage("SuccessPicture"), fileUpLoad);
        } catch (IOException e) {
            e.printStackTrace();
            return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
        }
    }
 
    /*
    * 公司Logo上传
    * */
    @SaCheckLogin
    @PostMapping("/uploadCompanyLogo")
    public ReturnMessageUpload uploadUserImg(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
        }
        try {
            // 生成唯一的文件名,避免文件名冲突
            String fileName = UUID.randomUUID().toString() + "." + getFileExtension(file.getOriginalFilename());
            // 构建文件保存路径
            Path filePath = Paths.get(uploadDirCompanyLogo, fileName);
            // 检查上传目录是否存在,如果不存在则创建
            File dir = new File(uploadDirCompanyLogo);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            // 将上传的文件保存到指定路径
            Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
            FileUpLoad fileUpLoad = new FileUpLoad();
            fileUpLoad.setFileUrl(uploadIp+"CompanyLogo/"+fileName);
            return new ReturnMessageUpload(200, MessageUtils.getMessage("SuccessPicture"), fileUpLoad);
        } catch (IOException e) {
            e.printStackTrace();
            return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
        }
    }
 
    /*
     * 监控图标上传
     * */
    @SaCheckLogin
    @PostMapping("/uploadHksxtIcon")
    public ReturnMessageUpload uploadHksxtIcon(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
        }
        try {
            // 生成唯一的文件名,避免文件名冲突
            String fileName = UUID.randomUUID().toString() + "." + getFileExtension(file.getOriginalFilename());
            // 构建文件保存路径
            Path filePath = Paths.get(uploadDirHksxtIcon, fileName);
            // 检查上传目录是否存在,如果不存在则创建
            File dir = new File(uploadDirHksxtIcon);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            // 将上传的文件保存到指定路径
            Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
            FileUpLoad fileUpLoad = new FileUpLoad();
            fileUpLoad.setFileUrl(uploadIp+"Hksxt/"+fileName);
            return new ReturnMessageUpload(200, MessageUtils.getMessage("SuccessPicture"), fileUpLoad);
        } catch (IOException e) {
            e.printStackTrace();
            return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
        }
    }
 
    private String getFileExtension(String fileName) {
        if (fileName != null && fileName.lastIndexOf(".") != -1) {
            return fileName.substring(fileName.lastIndexOf(".") + 1);
        }
        return "";
    }
}