liurunyu
2024-10-22 0561f9033014f831d7193a3222539b44b991dda8
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package com.dy.pipIrrGlobal.dyFile;
 
import com.dy.common.util.NumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.Arrays;
import java.util.List;
 
@Component
public class FileOperate {
    
    @Autowired
    private RestTemplate restTemplate ;
 
    /**
     * 拆分上载文件的名称
     * @param file
     * @return
     */
    public String[] splitFileName(MultipartFile file){
        String[] grp = new String[2] ;
        if(file != null) {
            String fileName = file.getOriginalFilename();
            int lastDotIndex = fileName.lastIndexOf('.');
            if (lastDotIndex >= 0) {
                grp[0] = fileName.substring(0, lastDotIndex);
                grp[1] = fileName.substring(lastDotIndex + 1);
                if(grp[0].trim().equals("")){
                    grp[0] = "匿名" ;
                }
            }
        }
        return grp ;
    }
 
    /**
     * 得到上载的文件扩展名
     * @param file
     * @return
     */
    public String getFileExtName(MultipartFile file){
        String fileExtName = null ;
        if(file != null) {
            String filename = file.getOriginalFilename();
            int lastDotIndex = filename.lastIndexOf('.');
            if (lastDotIndex >= 0) {
                fileExtName = filename.substring(lastDotIndex + 1);
            }
        }
        return fileExtName ;
    }
 
    /**
     * 得到上载的文件主名
     * @param fileName
     * @return
     */
    public String getFileMainName(String fileName){
        String fileMainName = null ;
        if(fileName != null) {
            int lastDotIndex = fileName.lastIndexOf('.');
            if (lastDotIndex >= 0) {
                fileMainName =  fileName.substring(0, lastDotIndex);
            }
        }
        if(fileMainName == null){
            fileMainName = "noName" ;
        }
        return fileMainName ;
    }
 
    /**
     * 通过照片路径,得到对应缩略图的路径
     * @param imgPath
     * @return
     */
    public String getImgFileZipPath(String imgPath){
        String path_ = null ;
        String prePath = null ;
        String tailPath = null ;
        if(imgPath != null && !imgPath.trim().equals("")) {
            int lastDotIndex = imgPath.lastIndexOf('.');
            if (lastDotIndex >= 0) {
                prePath = imgPath.substring(0, lastDotIndex);
                tailPath = imgPath.substring(lastDotIndex);
                path_ = prePath + "_" + tailPath ;
            }
        }
        if(path_ == null){
            path_ = imgPath ;
        }
        return path_ ;
    }
 
    /**
     * web分布式文件系统保存文件
     * @param file
     * @param fmUrl
     * @param fileCtrlRqMp
     * @param fileMethodMp
     * @param regionNum
     * @param fileExtName
     * @param json
     * @return
     * @throws Exception
     */
    public FileRestVo saveFile(MultipartFile file,
                               String fmUrl,
                               String fileCtrlRqMp,
                               String fileMethodMp,
                               String regionNum,
                               String fileExtName,
                               String json) throws Exception{
        FileRestVo rvo  = null ;
        if(file != null && file.getBytes() != null && file.getBytes().length > 0){
            rvo = this.restCreateFileName(fmUrl, fileExtName) ;
            if(rvo != null){
                String relativeFilePath = this.restSaveFile(fileCtrlRqMp, fileMethodMp, file, regionNum, json, rvo);
                rvo.fileWebPath = relativeFilePath ;
                //if(relativeFilePath != null){
                //    rvo.createFilePath(relativeFilePath, rvo.fileNameHash);
                //}
            }
        }
        return rvo ;
    }
 
    /**
     * 生成文件名称
     * @return
     */
    private FileRestVo restCreateFileName(String fmUrl, String fileExtName) throws Exception{
        // 准备请求数据
        MultiValueMap<String, Object> multipartRequestData = new LinkedMultiValueMap<>();
        multipartRequestData.add(FileConstant.fmPostMapping_create_paramName, fileExtName);
 
        // 设置请求头部,这里假设服务器接收multipart/form-data类型的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
 
        // 封装请求体
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequestData, headers);
 
        String webUrl = fmUrl + "/" + FileConstant.fmRequestMapping + "/" + FileConstant.fmPostMapping_create;
        // 发送POST请求
        FileRestVo rvo = restTemplate.postForObject(webUrl, requestEntity, FileRestVo.class);
 
        return rvo ;
    }
 
    /**
     * 把文件存储到文件系统中
     * @param file
     * @param regionNum
     * @param json json数据
     * @param rvo
     * @return
     * @throws Exception
     */
    private String restSaveFile(String fileCtrlRqMp,
                                String fileMethodMp,
                                MultipartFile file,
                                String regionNum,
                                String json,
                                FileRestVo rvo) throws Exception{
        // 准备请求数据
        MultiValueMap<String, Object> multipartRequestData = new LinkedMultiValueMap<>();
        multipartRequestData.add(FileConstant.filePostMapping_paramName_file, file.getResource());
        multipartRequestData.add(FileConstant.filePostMapping_paramName_regionNum, regionNum);
        multipartRequestData.add(FileConstant.filePostMapping_paramName_json, (json==null?"":json));
        multipartRequestData.add(FileConstant.filePostMapping_paramName_absolutePath, rvo.fileSysAbsolutePath);
        multipartRequestData.add(FileConstant.filePostMapping_paramName_relativePath, rvo.fileSysRelativePath);
        multipartRequestData.add(FileConstant.filePostMapping_paramName_fileName, rvo.fileName);
 
        // 设置请求头部,这里假设服务器接收multipart/form-data类型的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        //headers.setContentLength(file.getSize());
 
        // 封装请求体
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequestData, headers);
 
        String fileRestUrl = rvo.fileSysRestUrl ;
        if(!fileRestUrl.endsWith("/") && !fileRestUrl.endsWith("\\\\")){
            fileRestUrl += "/" ;
        }
        fileRestUrl += (fileCtrlRqMp + "/" + fileMethodMp) ;
 
        // 发送POST请求
        return restTemplate.postForObject(fileRestUrl, requestEntity, Object.class).toString();
    }
 
    /**
     * 解析文件名称
     * @param fmUrl
     * @param filePath
     * @return
     */
    public FileRestVo parse(String fmUrl, String filePath){
        // 准备请求数据
        MultiValueMap<String, Object> multipartRequestData = new LinkedMultiValueMap<>();
        multipartRequestData.add(FileConstant.fmPostMapping_parsePath_paramName, filePath);
 
        // 设置请求头部,这里假设服务器接收multipart/form-data类型的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
 
        // 封装请求体
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequestData, headers);
 
        String webUrl = fmUrl + "/" + FileConstant.fmRequestMapping + "/" + FileConstant.fmPostMapping_parsePath;
        // 发送POST请求
        FileRestVo rvo = restTemplate.postForObject(webUrl, requestEntity, FileRestVo.class);
 
        return rvo ;
    }
 
 
    /**
     * 解析文件名称
     * @param fmUrl
     * @param filePaths
     * @return
     */
    public List<FileRestVo> parse(String fmUrl, List<String> filePaths) throws Exception{
        List<FileRestVo> rList = null ;
        if(filePaths != null && filePaths.size() > 0) {
            MultiValueMap<String, Object> multipartRequestData = new LinkedMultiValueMap<>();
            multipartRequestData.add(FileConstant.fmPostMapping_parsePathList_paramName, filePaths);
 
            // 设置请求头部,这里假设服务器接收multipart/form-data类型的数据
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
 
            // 封装请求体
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequestData, headers);
 
            String webUrl = fmUrl + "/" + FileConstant.fmRequestMapping + "/" + FileConstant.fmPostMapping_parsePathList;
            // 发送POST请求
            FileRestVo[] rvos = restTemplate.postForObject(webUrl, requestEntity, FileRestVo[].class);
            rList = Arrays.asList(rvos) ;
        }
        return rList ;
    }
 
    /**
     * 解析文件哈希值
     * @param fmUrl
     * @param hashcode
     * @return
     */
    public FileRestVo parseHashcode(String fmUrl, Integer hashcode){
        // 准备请求数据
        MultiValueMap<String, Object> multipartRequestData = new LinkedMultiValueMap<>();
        multipartRequestData.add(FileConstant.fmPostMapping_parseHashcode_paramName, hashcode);
 
        // 设置请求头部,这里假设服务器接收multipart/form-data类型的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
 
        // 封装请求体
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequestData, headers);
 
        String webUrl = fmUrl + "/" + FileConstant.fmRequestMapping + "/" + FileConstant.fmPostMapping_parseHashcode;
        // 发送POST请求
        FileRestVo rvo = restTemplate.postForObject(webUrl, requestEntity, FileRestVo.class);
 
        return rvo ;
    }
 
    /**
     * 解析文件哈希值
     * @param fmUrl
     * @param hashCodes
     * @return
     */
    public  List<FileRestVo> parseHashcode(String fmUrl, List<Integer> hashCodes){
        List<FileRestVo> rList = null ;
        if(hashCodes != null && hashCodes.size() > 0) {
            // 准备请求数据
            MultiValueMap<String, Object> multipartRequestData = new LinkedMultiValueMap<>();
            multipartRequestData.add(FileConstant.fmPostMapping_parseHashcodeList_paramName, hashCodes);
 
            // 设置请求头部,这里假设服务器接收multipart/form-data类型的数据
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
 
            // 封装请求体
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequestData, headers);
 
            String webUrl = fmUrl + "/" + FileConstant.fmRequestMapping + "/" + FileConstant.fmPostMapping_parseHashcodeList;
            // 发送POST请求
            FileRestVo[] rvos = restTemplate.postForObject(webUrl, requestEntity, FileRestVo[].class);
            rList = Arrays.asList(rvos) ;
        }
        return rList ;
    }
 
    /**
     * 解析文件文件路径中的哈希值并返回图片完整路径
     * @param fmUrl
     * @param filePath
     * @return
     */
    public String getFilePath(String fmUrl, String filePath){
        FileRestVo rvo = null ;
        if(filePath != null && !filePath.trim().equals("")){
            String[] strs = filePath.split("\\?") ;
            String hashValStr = strs[strs.length - 1] ;
            if(hashValStr != null && !hashValStr.trim().equals("") && NumUtil.isPlusIntNumber(hashValStr)){
                int hashVal = Integer.valueOf(hashValStr) ;
                rvo = parseHashcode(fmUrl, hashVal) ;
            }
        }
        if(rvo != null){
            return rvo.getFileWebUrl() + filePath ;
        }
        return null ;
    }
 
}