|
|
@@ -1,8 +1,9 @@
|
|
1
|
1
|
package api.controller.common;
|
|
2
|
2
|
|
|
3
|
|
-import com.alibaba.excel.util.StringUtils;
|
|
4
|
3
|
import api.controller.BaseController;
|
|
5
|
4
|
import api.model.AjaxResult;
|
|
|
5
|
+import api.util.helper.DateHelper;
|
|
|
6
|
+import api.util.helper.FileUploadHelper;
|
|
6
|
7
|
import io.swagger.annotations.ApiOperation;
|
|
7
|
8
|
import lombok.extern.slf4j.Slf4j;
|
|
8
|
9
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
@@ -18,7 +19,6 @@ import java.net.URL;
|
|
18
|
19
|
import java.net.URLConnection;
|
|
19
|
20
|
import java.net.URLEncoder;
|
|
20
|
21
|
import java.util.HashMap;
|
|
21
|
|
-import java.util.UUID;
|
|
22
|
22
|
|
|
23
|
23
|
/**
|
|
24
|
24
|
* 通用请求处理
|
|
|
@@ -39,57 +39,24 @@ public class CommonController extends BaseController {
|
|
39
|
39
|
if (files == null || files.length == 0) {
|
|
40
|
40
|
return Error("无文件");
|
|
41
|
41
|
}
|
|
42
|
|
- //新文件名
|
|
43
|
|
- StringBuilder newFileName;
|
|
44
|
|
-
|
|
45
|
|
- //全部文件名,若多个,则逗号分隔
|
|
46
|
|
- StringBuilder allFileNames = new StringBuilder();
|
|
47
|
|
-
|
|
48
|
42
|
//上传路径
|
|
49
|
|
- String fileSavePath = "files";
|
|
50
|
|
-
|
|
|
43
|
+ String fileSavePath = "files/common/"+ DateHelper.getDate();
|
|
51
|
44
|
//若文件夹不存在,则创建文件夹
|
|
52
|
45
|
File pFile = new File(fileSavePath);
|
|
53
|
|
- if (!pFile.exists()) {
|
|
54
|
|
- pFile.mkdirs();
|
|
55
|
|
- }
|
|
|
46
|
+ if (!pFile.exists()) { pFile.mkdirs(); }
|
|
56
|
47
|
|
|
|
48
|
+ HashMap<String, String> fileInfo =new HashMap<>();
|
|
57
|
49
|
//遍历所有文件
|
|
58
|
50
|
for (MultipartFile file : files) {
|
|
59
|
|
- //得到原始文件名
|
|
60
|
|
- newFileName = new StringBuilder(file.getOriginalFilename());
|
|
61
|
|
- //查询文件名中点号所处位置
|
|
62
|
|
- int i = newFileName.lastIndexOf(".");
|
|
63
|
|
- //若点号位置大于0,则文件名正常
|
|
64
|
|
- if (i > 0) {
|
|
65
|
|
- //在点号前拼接UUID防止文件重名
|
|
66
|
|
- newFileName.insert(i, "_" + UUID.randomUUID().toString().substring(0, 5));
|
|
67
|
|
- } else {
|
|
68
|
|
- //若点号位置小于等于0,则文件名不规范
|
|
69
|
|
- return Error("文件'" + newFileName + "'名称不规范");
|
|
70
|
|
- }
|
|
|
51
|
+ String filePath="";
|
|
71
|
52
|
try {
|
|
72
|
|
- // 保存文件
|
|
73
|
|
- file.transferTo(new File(pFile.getAbsolutePath()+"/" + newFileName));
|
|
74
|
|
- //若全部文件名为空,则此为第一个文件,直接追加此文件名
|
|
75
|
|
- if (StringUtils.isBlank(allFileNames.toString())) {
|
|
76
|
|
- allFileNames.append(newFileName);
|
|
77
|
|
- } else {
|
|
78
|
|
- //若全部文件名不为空,则追加逗号和文件名
|
|
79
|
|
- allFileNames.append(",").append(newFileName).toString();
|
|
80
|
|
- }
|
|
|
53
|
+ filePath= FileUploadHelper.upload(fileSavePath,file);
|
|
81
|
54
|
} catch (IOException e) {
|
|
82
|
|
- log.error("上传失败",e);
|
|
83
|
|
- return Error("上传失败");
|
|
|
55
|
+ throw new RuntimeException(e);
|
|
84
|
56
|
}
|
|
|
57
|
+ fileInfo.put(file.getOriginalFilename(),filePath);
|
|
85
|
58
|
}
|
|
86
|
59
|
|
|
87
|
|
- //返回全部文件名和文件路径
|
|
88
|
|
- HashMap<String, String> fileInfo = new HashMap<String, String>(2) {{
|
|
89
|
|
- put("fileNames", allFileNames.toString());
|
|
90
|
|
- put("filePath", fileSavePath);
|
|
91
|
|
- }};
|
|
92
|
|
- //返回信息
|
|
93
|
60
|
return Success("上传成功", fileInfo);
|
|
94
|
61
|
}
|
|
95
|
62
|
|
|
|
@@ -105,8 +72,6 @@ public class CommonController extends BaseController {
|
|
105
|
72
|
File file = new File(path);
|
|
106
|
73
|
// 获取文件名
|
|
107
|
74
|
String filename = file.getName();
|
|
108
|
|
- // 获取文件后缀名
|
|
109
|
|
- String e1xt = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
|
|
110
|
75
|
|
|
111
|
76
|
// 将文件写入输入流
|
|
112
|
77
|
FileInputStream fileInputStream = new FileInputStream(file);
|