|
@@ -73,43 +73,66 @@ public class CommonController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 上传图片到aliyun OSS,返回图片的地址
|
|
|
+ * 上传单个图片到aliyun OSS,返回图片的地址
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PostMapping("upload/image")
|
|
|
+ public RestResponse uploadImage(@RequestParam("file") MultipartFile file) {
|
|
|
+ return upload(file,Values.Upload.pics,Values.Upload.maxSizePic);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传多个图片,返回图片地址集合list
|
|
|
* @param files
|
|
|
* @return
|
|
|
*/
|
|
|
@PostMapping("upload/images")
|
|
|
public RestResponse uploadImage(@RequestParam("file") MultipartFile[] files) {
|
|
|
- return upload(files,Values.Upload.pics,Values.Upload.maxSizePic);
|
|
|
+ return uploads(files,Values.Upload.pics,Values.Upload.maxSizePic);
|
|
|
}
|
|
|
|
|
|
- @PostMapping("upload/image")
|
|
|
- public RestResponse uploadImage(@RequestParam("file") MultipartFile file) {
|
|
|
- MultipartFile[] files = new MultipartFile[]{file};
|
|
|
- return upload(files,Values.Upload.pics,Values.Upload.maxSizePic);
|
|
|
+ /**
|
|
|
+ * 上传单个文件,返回文件地址
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PostMapping("upload/file")
|
|
|
+ public RestResponse uploadFile(@RequestParam("file") MultipartFile file) {
|
|
|
+ return upload(file,Values.Upload.files,Values.Upload.maxSizeFile);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 上传多个文件,返回文件地址集合list
|
|
|
+ * @param files
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@PostMapping("upload/files")
|
|
|
public RestResponse uploadFile(@RequestParam("file") MultipartFile[] files) {
|
|
|
- return upload(files,Values.Upload.files,Values.Upload.maxSizeFile);
|
|
|
- }
|
|
|
- @PostMapping("upload/file")
|
|
|
- public RestResponse uploadFile(@RequestParam("file") MultipartFile file) {
|
|
|
- MultipartFile[] files = new MultipartFile[]{file};
|
|
|
- return upload(files,Values.Upload.files,Values.Upload.maxSizeFile);
|
|
|
+ return uploads(files,Values.Upload.files,Values.Upload.maxSizeFile);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 上传单个视频,返回视频地址
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@PostMapping("upload/video")
|
|
|
public RestResponse uploadVideo(@RequestParam("file") MultipartFile file) {
|
|
|
- MultipartFile[] files = new MultipartFile[]{file};
|
|
|
- return upload(files,Values.Upload.videos,Values.Upload.maxSizeVideo);
|
|
|
+ return upload(file,Values.Upload.videos,Values.Upload.maxSizeVideo);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 上传多个视频,返回视频地址集合list
|
|
|
+ * @param files
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@PostMapping("upload/videos")
|
|
|
public RestResponse uploadVideo(@RequestParam("file") MultipartFile[] files) {
|
|
|
- return upload(files,Values.Upload.videos,Values.Upload.maxSizeVideo);
|
|
|
+ return uploads(files,Values.Upload.videos,Values.Upload.maxSizeVideo);
|
|
|
}
|
|
|
|
|
|
- private RestResponse upload(MultipartFile[] files,String[] types,long maxSize){
|
|
|
+ private RestResponse uploads(MultipartFile[] files,String[] types,long maxSize){
|
|
|
try {
|
|
|
Judge.notNull(files,Values.Upload.missingFileData);
|
|
|
List<OssEntity> ossEntities = new ArrayList<>();
|
|
@@ -139,4 +162,26 @@ public class CommonController {
|
|
|
return RestResponse.fail(Values.Upload.fileFail);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private RestResponse upload(MultipartFile file,String[] types,long maxSize){
|
|
|
+ try {
|
|
|
+ Judge.notTrue(null == file || file.isEmpty(),Values.Upload.missingFileData);
|
|
|
+ Judge.notTrue(file.getSize() > maxSize,"文件大小不超过" + (maxSize >> 20) + "M");
|
|
|
+ // 获取文件名,带后缀
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ log.info("上传文件,原文件名:" + originalFilename);
|
|
|
+ // 获取文件的后缀格式
|
|
|
+ String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
|
|
|
+ Judge.isTrue(StrUtil.equalsAny(fileSuffix,types),"文件格式不支持, 暂时只支持" + types.toString());
|
|
|
+ String filename = StrUtil.format("{}/{}{}",DateUtil.format(DateUtil.date(),"yyyy/MM/dd"),IdWorker.getIdStr(),fileSuffix);
|
|
|
+ log.info("上传文件,新文件名:" + filename);
|
|
|
+ AliyunOSS aliyunOSS = systemConfiguration.build();
|
|
|
+ String url = aliyunOSS.putObject(systemConfiguration.getBucketName(), filename, file.getBytes());
|
|
|
+ log.info("上传文件,文件URL: {}",url);
|
|
|
+ return Judge.result(url);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("上传文件异常:{}",e);
|
|
|
+ return RestResponse.fail(Values.Upload.fileFail);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|