|
1
|
package com.ruoyi.web.controller.common;
|
1
|
package com.ruoyi.web.controller.common;
|
|
2
|
|
2
|
|
|
|
|
3
|
+import java.util.ArrayList;
|
|
|
|
4
|
+import java.util.List;
|
|
3
|
import javax.servlet.http.HttpServletRequest;
|
5
|
import javax.servlet.http.HttpServletRequest;
|
|
4
|
import javax.servlet.http.HttpServletResponse;
|
6
|
import javax.servlet.http.HttpServletResponse;
|
|
5
|
import org.slf4j.Logger;
|
7
|
import org.slf4j.Logger;
|
|
@@ -8,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
@@ -8,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
8
|
import org.springframework.http.MediaType;
|
10
|
import org.springframework.http.MediaType;
|
|
9
|
import org.springframework.web.bind.annotation.GetMapping;
|
11
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
10
|
import org.springframework.web.bind.annotation.PostMapping;
|
12
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
13
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
11
|
import org.springframework.web.bind.annotation.RestController;
|
14
|
import org.springframework.web.bind.annotation.RestController;
|
|
12
|
import org.springframework.web.multipart.MultipartFile;
|
15
|
import org.springframework.web.multipart.MultipartFile;
|
|
13
|
import com.ruoyi.common.config.RuoYiConfig;
|
16
|
import com.ruoyi.common.config.RuoYiConfig;
|
|
@@ -24,6 +27,7 @@ import com.ruoyi.framework.config.ServerConfig; |
|
@@ -24,6 +27,7 @@ import com.ruoyi.framework.config.ServerConfig; |
|
24
|
* @author ruoyi
|
27
|
* @author ruoyi
|
|
25
|
*/
|
28
|
*/
|
|
26
|
@RestController
|
29
|
@RestController
|
|
|
|
30
|
+@RequestMapping("/common")
|
|
27
|
public class CommonController
|
31
|
public class CommonController
|
|
28
|
{
|
32
|
{
|
|
29
|
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
|
33
|
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
|
|
@@ -31,13 +35,15 @@ public class CommonController |
|
@@ -31,13 +35,15 @@ public class CommonController |
|
31
|
@Autowired
|
35
|
@Autowired
|
|
32
|
private ServerConfig serverConfig;
|
36
|
private ServerConfig serverConfig;
|
|
33
|
|
37
|
|
|
|
|
38
|
+ private static final String FILE_DELIMETER = ",";
|
|
|
|
39
|
+
|
|
34
|
/**
|
40
|
/**
|
|
35
|
* 通用下载请求
|
41
|
* 通用下载请求
|
|
36
|
*
|
42
|
*
|
|
37
|
* @param fileName 文件名称
|
43
|
* @param fileName 文件名称
|
|
38
|
* @param delete 是否删除
|
44
|
* @param delete 是否删除
|
|
39
|
*/
|
45
|
*/
|
|
40
|
- @GetMapping("common/download")
|
46
|
+ @GetMapping("/download")
|
|
41
|
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
|
47
|
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
|
|
42
|
{
|
48
|
{
|
|
43
|
try
|
49
|
try
|
|
@@ -64,9 +70,9 @@ public class CommonController |
|
@@ -64,9 +70,9 @@ public class CommonController |
|
64
|
}
|
70
|
}
|
|
65
|
|
71
|
|
|
66
|
/**
|
72
|
/**
|
|
67
|
- * 通用上传请求
|
73
|
+ * 通用上传请求(单个)
|
|
68
|
*/
|
74
|
*/
|
|
69
|
- @PostMapping("/common/upload")
|
75
|
+ @PostMapping("/upload")
|
|
70
|
public AjaxResult uploadFile(MultipartFile file) throws Exception
|
76
|
public AjaxResult uploadFile(MultipartFile file) throws Exception
|
|
71
|
{
|
77
|
{
|
|
72
|
try
|
78
|
try
|
|
@@ -77,8 +83,47 @@ public class CommonController |
|
@@ -77,8 +83,47 @@ public class CommonController |
|
77
|
String fileName = FileUploadUtils.upload(filePath, file);
|
83
|
String fileName = FileUploadUtils.upload(filePath, file);
|
|
78
|
String url = serverConfig.getUrl() + fileName;
|
84
|
String url = serverConfig.getUrl() + fileName;
|
|
79
|
AjaxResult ajax = AjaxResult.success();
|
85
|
AjaxResult ajax = AjaxResult.success();
|
|
80
|
- ajax.put("fileName", fileName);
|
|
|
|
81
|
ajax.put("url", url);
|
86
|
ajax.put("url", url);
|
|
|
|
87
|
+ ajax.put("fileName", fileName);
|
|
|
|
88
|
+ ajax.put("newFileName", FileUtils.getName(fileName));
|
|
|
|
89
|
+ ajax.put("originalFilename", file.getOriginalFilename());
|
|
|
|
90
|
+ return ajax;
|
|
|
|
91
|
+ }
|
|
|
|
92
|
+ catch (Exception e)
|
|
|
|
93
|
+ {
|
|
|
|
94
|
+ return AjaxResult.error(e.getMessage());
|
|
|
|
95
|
+ }
|
|
|
|
96
|
+ }
|
|
|
|
97
|
+
|
|
|
|
98
|
+ /**
|
|
|
|
99
|
+ * 通用上传请求(多个)
|
|
|
|
100
|
+ */
|
|
|
|
101
|
+ @PostMapping("/uploads")
|
|
|
|
102
|
+ public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception
|
|
|
|
103
|
+ {
|
|
|
|
104
|
+ try
|
|
|
|
105
|
+ {
|
|
|
|
106
|
+ // 上传文件路径
|
|
|
|
107
|
+ String filePath = RuoYiConfig.getUploadPath();
|
|
|
|
108
|
+ List<String> urls = new ArrayList<String>();
|
|
|
|
109
|
+ List<String> fileNames = new ArrayList<String>();
|
|
|
|
110
|
+ List<String> newFileNames = new ArrayList<String>();
|
|
|
|
111
|
+ List<String> originalFilenames = new ArrayList<String>();
|
|
|
|
112
|
+ for (MultipartFile file : files)
|
|
|
|
113
|
+ {
|
|
|
|
114
|
+ // 上传并返回新文件名称
|
|
|
|
115
|
+ String fileName = FileUploadUtils.upload(filePath, file);
|
|
|
|
116
|
+ String url = serverConfig.getUrl() + fileName;
|
|
|
|
117
|
+ urls.add(url);
|
|
|
|
118
|
+ fileNames.add(fileName);
|
|
|
|
119
|
+ newFileNames.add(FileUtils.getName(fileName));
|
|
|
|
120
|
+ originalFilenames.add(file.getOriginalFilename());
|
|
|
|
121
|
+ }
|
|
|
|
122
|
+ AjaxResult ajax = AjaxResult.success();
|
|
|
|
123
|
+ ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
|
|
|
|
124
|
+ ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
|
|
|
|
125
|
+ ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
|
|
|
|
126
|
+ ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
|
|
82
|
return ajax;
|
127
|
return ajax;
|
|
83
|
}
|
128
|
}
|
|
84
|
catch (Exception e)
|
129
|
catch (Exception e)
|
|
@@ -90,7 +135,7 @@ public class CommonController |
|
@@ -90,7 +135,7 @@ public class CommonController |
|
90
|
/**
|
135
|
/**
|
|
91
|
* 本地资源通用下载
|
136
|
* 本地资源通用下载
|
|
92
|
*/
|
137
|
*/
|
|
93
|
- @GetMapping("/common/download/resource")
|
138
|
+ @GetMapping("/download/resource")
|
|
94
|
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
|
139
|
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
|
|
95
|
throws Exception
|
140
|
throws Exception
|
|
96
|
{
|
141
|
{
|