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
package cn.wisenergy.chnmuseum.party.web.controller;
import cn.wisenergy.chnmuseum.party.common.dfs.FastDFSUtils;
import cn.wisenergy.chnmuseum.party.common.enums.FileTypeEnum;
import cn.wisenergy.chnmuseum.party.common.enums.HANDLE_STATUS_ENUM;
import cn.wisenergy.chnmuseum.party.common.enums.RESPONSE_CODE_ENUM;
import cn.wisenergy.chnmuseum.party.common.mvc.InterfaceException;
import cn.wisenergy.chnmuseum.party.common.vo.BatchUploadResVO;
import cn.wisenergy.chnmuseum.party.common.vo.ImageUploadResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* <p>
* 文件上传 前端控制器
* </p>
*
* @author Danny Lee
* @since 2021-03-16
*/
@Slf4j
@RestController
@RequestMapping("/file")
public class FileUploadController {
// 允许上传的格式
private static final String[] IMAGE_TYPE = new String[]{"JPG", "JPEG", "PNG", "BMP", "WBMP"};
// 允许上传的格式
private static final String[] VIDEO_TYPE = new String[]{"MP4", "FLV"};
// 允许上传的格式
private static final String[] DATUM_TYPE = new String[]{"PDF", "DOC", "DOCX", "XLS", "XLSX", "PPT", "PPTX", "JPG", "JPEG", "PNG", "BMP", "WBMP", "MP4", "FLV"};
// @RequestMapping(value = "/upload", method = RequestMethod.POST)
// public ResponseEntity<PicUploadResult> upload(@RequestParam(value = "bc_cover", required = true) MultipartFile uploadFile, HttpServletResponse response) throws Exception {
// // 校验文件扩展名
// boolean isLegal = false;
// for (String type : IMAGE_TYPE) {
// if (StringUtils.endsWithIgnoreCase(uploadFile.getOriginalFilename(), type)) {
// isLegal = true;
// break;
// }
// }
// byte[] bytes = uploadFile.getBytes();
// if (ImageUtil.isPicture(uploadFile.getOriginalFilename()) && ImageUtil.isImage(bytes) && ImageUtil.isImage1(bytes)) {
// isLegal = true;
// } else {
// isLegal = false;
// throw new RuntimeException("图片文件不合法");
// }
//
// // 封装Result对象,并且将文件的byte数组放置到result对象中
// PicUploadResult fileUploadResult = new PicUploadResult();
// // 状态
// fileUploadResult.setError(isLegal ? 0 : 1);
// // 文件新路径
// String filePath = FastDFSUtils.uploadPic(bytes, uploadFile.getOriginalFilename(), uploadFile.getSize());
//
// if (log.isDebugEnabled()) {
// log.debug("Pic file upload .[{}] to [{}] .", uploadFile.getOriginalFilename(), filePath);
// }
// String picUrl = IMAGE_BASE_URL + filePath;
// fileUploadResult.setUrl(picUrl);
// isLegal = false;
// try {
// BufferedImage image = ImageIO.read(new URL(picUrl));
// if (image != null) {
// // fileUploadResult.setWidth(image.getWidth() + "");
// // fileUploadResult.setHeight(image.getHeight() + "");
// isLegal = true;
// }
// } catch (IOException e) {
// log.error("check image is legal error! " + e.getMessage());
// }
// isLegal = true;
// fileUploadResult.setError(isLegal ? 0 : 1);
// if (!isLegal) {
// // 不合法,将磁盘上的文件删除
// FastDFSUtils.deletePic(picUrl);
// }
// return ResponseEntity.status(HttpStatus.CREATED).body(fileUploadResult);
// }
@ApiImplicitParams({
@ApiImplicitParam(name = "files", value = "资料", paramType = "form", dataType = "__file", collectionFormat = "array", allowMultiple = true)
})
@PostMapping(value = "/file/upload", headers = "content-type=multipart/form-data", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ApiOperation(value = "资料上传", notes = "资料上传")
public ResponseEntity<BatchUploadResVO> uploadFile(@RequestPart(value = "files", required = false) MultipartFile[] files) throws IOException {
if (files.length == 0) {
throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "没有文件可供上传");
}
int successCount = 0;
int failureCount = 0;
List<BatchUploadResVO.HandleResult> handleList = new ArrayList<>();
List<String> videoUrlList = new ArrayList<>();
for (MultipartFile file : files) {
// 当前维度表下线结果
BatchUploadResVO.HandleResult handleResult = new BatchUploadResVO.HandleResult();
// 原始文件名
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
handleResult.setFileName("");
handleResult.setFileType(FileTypeEnum.DATUM.getName());
handleResult.setFileUrl("");
handleResult.setHandleResult(HANDLE_STATUS_ENUM.FAILURE.getName());
handleResult.setDescription("文件名为空");
failureCount++;
continue;
}
handleResult.setFileName(originalFilename);
handleResult.setFileType(FileTypeEnum.DATUM.getName());
handleResult.setHandleResult(HANDLE_STATUS_ENUM.SUCCESS.getName());
boolean anyMatch = Arrays.stream(DATUM_TYPE).anyMatch(s -> Objects.equals(s, FilenameUtils.getExtension(originalFilename).toUpperCase()));
if (anyMatch) {
String url = FastDFSUtils.uploadFile(file.getInputStream(), file.getSize(), originalFilename);
handleResult.setFileUrl(url);
handleResult.setDescription("操作成功");
videoUrlList.add(url);
successCount++;
} else {
handleResult.setFileUrl("");
handleResult.setDescription("文件格式不支持");
failureCount++;
}
// 设置处理的业务表信息
handleList.add(handleResult);
}
BatchUploadResVO batchUploadResVO = new BatchUploadResVO();
batchUploadResVO.setFailureCount(failureCount);
batchUploadResVO.setSuccessCount(successCount);
batchUploadResVO.setTotal(files.length);
batchUploadResVO.setHandleList(handleList);
batchUploadResVO.setUrlList(videoUrlList);
return ResponseEntity.status(HttpStatus.CREATED).body(batchUploadResVO);
}
@PostMapping(value = "/image/upload", headers = "content-type=multipart/form-data", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@RequiresPermissions("image:upload")
@ApiOperation(value = "单图片上传", notes = "单图片上传")
public ResponseEntity<ImageUploadResult> uploadImage(@RequestParam(value = "cover") MultipartFile uploadFile) throws Exception {
if (uploadFile == null || uploadFile.getSize() == 0) {
throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "没有文件可供上传");
}
String fileName = uploadFile.getOriginalFilename();
String extension = FilenameUtils.getExtension(fileName);
if (StringUtils.isBlank(extension)) {
throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "文件格式不支持");
}
boolean anyMatch = Arrays.stream(IMAGE_TYPE).anyMatch(s -> Objects.equals(s, extension.toUpperCase()));
if (!anyMatch) {
throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "文件格式不支持");
}
String url = FastDFSUtils.uploadFile(uploadFile.getInputStream(), uploadFile.getSize(), fileName);
ImageUploadResult imageUploadResult = new ImageUploadResult();
imageUploadResult.setFileName(fileName);
imageUploadResult.setFileExtName(extension);
imageUploadResult.setFileSize(uploadFile.getSize());
imageUploadResult.setUrl(url);
return ResponseEntity.status(HttpStatus.CREATED).body(imageUploadResult);
}
@ApiImplicitParams({
@ApiImplicitParam(name = "files", value = "视频文件", paramType = "form", dataType = "__file", collectionFormat = "array", allowMultiple = true)
})
@PostMapping(value = "/video/upload", headers = "content-type=multipart/form-data", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@RequiresPermissions("video:upload")
@ApiOperation(value = "多视频上传", notes = "多视频上传")
public ResponseEntity<BatchUploadResVO> uploadVideo(@RequestPart(value = "files", required = false) MultipartFile[] files) throws IOException {
if (files.length == 0) {
throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "没有文件可供上传");
}
final boolean matchChinese = Arrays.stream(files).anyMatch(s -> Objects.requireNonNull(s.getOriginalFilename()).contains("中文"));
if (!matchChinese) {
throw new InterfaceException(RESPONSE_CODE_ENUM.REQUEST_PARAMS_ERROR.getCode(), "必须包含中文视频");
}
int successCount = 0;
int failureCount = 0;
List<BatchUploadResVO.HandleResult> handleList = new ArrayList<>();
List<String> videoUrlList = new ArrayList<>();
for (MultipartFile file : files) {
// 当前维度表下线结果
BatchUploadResVO.HandleResult handleResult = new BatchUploadResVO.HandleResult();
// 原始文件名
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
handleResult.setFileName("");
handleResult.setFileType(FileTypeEnum.VIDEO.getName());
handleResult.setFileUrl("");
handleResult.setHandleResult(HANDLE_STATUS_ENUM.FAILURE.getName());
handleResult.setDescription("文件名为空");
failureCount++;
continue;
}
handleResult.setFileName(originalFilename);
handleResult.setFileType(FileTypeEnum.VIDEO.getName());
handleResult.setHandleResult(HANDLE_STATUS_ENUM.SUCCESS.getName());
boolean anyMatch = Arrays.stream(VIDEO_TYPE).anyMatch(s -> Objects.equals(s, FilenameUtils.getExtension(originalFilename).toUpperCase()));
if (anyMatch) {
String url = FastDFSUtils.uploadVideo(file.getInputStream(), file.getSize(), originalFilename);
handleResult.setFileUrl(url);
handleResult.setDescription("操作成功");
videoUrlList.add(url);
successCount++;
} else {
handleResult.setFileUrl("");
handleResult.setDescription("文件格式不支持");
failureCount++;
}
// 设置处理的业务表信息
handleList.add(handleResult);
}
BatchUploadResVO batchUploadResVO = new BatchUploadResVO();
batchUploadResVO.setFailureCount(failureCount);
batchUploadResVO.setSuccessCount(successCount);
batchUploadResVO.setTotal(files.length);
batchUploadResVO.setHandleList(handleList);
batchUploadResVO.setUrlList(videoUrlList);
return ResponseEntity.status(HttpStatus.CREATED).body(batchUploadResVO);
}
}