|
@@ -1,6 +1,7 @@
|
|
package com.lovecoding.upload.controller;
|
|
package com.lovecoding.upload.controller;
|
|
|
|
|
|
import com.lovecoding.upload.config.FastDFSClientUtil;
|
|
import com.lovecoding.upload.config.FastDFSClientUtil;
|
|
|
|
+import com.lovecoding.upload.utils.CommonResult;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -9,6 +10,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+
|
|
@RestController
|
|
@RestController
|
|
@RequestMapping("/file")
|
|
@RequestMapping("/file")
|
|
public class FileController {
|
|
public class FileController {
|
|
@@ -20,23 +23,24 @@ public class FileController {
|
|
private String fileServerUrl ;
|
|
private String fileServerUrl ;
|
|
|
|
|
|
@PostMapping("/upload")
|
|
@PostMapping("/upload")
|
|
- public String uploadFile(MultipartFile file) {
|
|
|
|
|
|
+ public CommonResult uploadFile(MultipartFile file) {
|
|
|
|
+
|
|
try {
|
|
try {
|
|
//判断文件是否存在
|
|
//判断文件是否存在
|
|
if (file == null) {
|
|
if (file == null) {
|
|
- throw new RuntimeException("文件不存在");
|
|
|
|
|
|
+ return CommonResult.failed("file is require");
|
|
}
|
|
}
|
|
//获取文件的完整名称
|
|
//获取文件的完整名称
|
|
String originalFilename = file.getOriginalFilename();
|
|
String originalFilename = file.getOriginalFilename();
|
|
if (StringUtils.isEmpty(originalFilename)) {
|
|
if (StringUtils.isEmpty(originalFilename)) {
|
|
- throw new RuntimeException("文件不存在");
|
|
|
|
|
|
+ return CommonResult.failed("file not find");
|
|
}
|
|
}
|
|
String url = fastDFSClientUtil.uploadFile(file);
|
|
String url = fastDFSClientUtil.uploadFile(file);
|
|
- return fileServerUrl+url;
|
|
|
|
|
|
+ return CommonResult.success(fileServerUrl+url);
|
|
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
- return "文件上传失败";
|
|
|
|
|
|
+ return CommonResult.failed("文件上传失败");
|
|
}
|
|
}
|
|
}
|
|
}
|