|
|
@@ -4,6 +4,7 @@ import java.util.ArrayList;
|
|
4
|
4
|
import java.util.List;
|
|
5
|
5
|
import javax.servlet.http.HttpServletRequest;
|
|
6
|
6
|
import javax.servlet.http.HttpServletResponse;
|
|
|
7
|
+
|
|
7
|
8
|
import org.slf4j.Logger;
|
|
8
|
9
|
import org.slf4j.LoggerFactory;
|
|
9
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -22,13 +23,12 @@ import com.sundot.airport.framework.config.ServerConfig;
|
|
22
|
23
|
|
|
23
|
24
|
/**
|
|
24
|
25
|
* 通用请求处理
|
|
25
|
|
- *
|
|
|
26
|
+ *
|
|
26
|
27
|
* @author ruoyi
|
|
27
|
28
|
*/
|
|
28
|
29
|
@RestController
|
|
29
|
30
|
@RequestMapping("/common")
|
|
30
|
|
-public class CommonController
|
|
31
|
|
-{
|
|
|
31
|
+public class CommonController {
|
|
32
|
32
|
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
|
|
33
|
33
|
|
|
34
|
34
|
@Autowired
|
|
|
@@ -38,17 +38,14 @@ public class CommonController
|
|
38
|
38
|
|
|
39
|
39
|
/**
|
|
40
|
40
|
* 通用下载请求
|
|
41
|
|
- *
|
|
|
41
|
+ *
|
|
42
|
42
|
* @param fileName 文件名称
|
|
43
|
43
|
* @param delete 是否删除
|
|
44
|
44
|
*/
|
|
45
|
45
|
@GetMapping("/download")
|
|
46
|
|
- public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
|
|
47
|
|
- {
|
|
48
|
|
- try
|
|
49
|
|
- {
|
|
50
|
|
- if (!FileUtils.checkAllowDownload(fileName))
|
|
51
|
|
- {
|
|
|
46
|
+ public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
|
|
|
47
|
+ try {
|
|
|
48
|
+ if (!FileUtils.checkAllowDownload(fileName)) {
|
|
52
|
49
|
throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
|
|
53
|
50
|
}
|
|
54
|
51
|
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
|
|
|
@@ -57,13 +54,10 @@ public class CommonController
|
|
57
|
54
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
|
58
|
55
|
FileUtils.setAttachmentResponseHeader(response, realFileName);
|
|
59
|
56
|
FileUtils.writeBytes(filePath, response.getOutputStream());
|
|
60
|
|
- if (delete)
|
|
61
|
|
- {
|
|
|
57
|
+ if (delete) {
|
|
62
|
58
|
FileUtils.deleteFile(filePath);
|
|
63
|
59
|
}
|
|
64
|
|
- }
|
|
65
|
|
- catch (Exception e)
|
|
66
|
|
- {
|
|
|
60
|
+ } catch (Exception e) {
|
|
67
|
61
|
log.error("下载文件失败", e);
|
|
68
|
62
|
}
|
|
69
|
63
|
}
|
|
|
@@ -72,10 +66,8 @@ public class CommonController
|
|
72
|
66
|
* 通用上传请求(单个)
|
|
73
|
67
|
*/
|
|
74
|
68
|
@PostMapping("/upload")
|
|
75
|
|
- public AjaxResult uploadFile(MultipartFile file) throws Exception
|
|
76
|
|
- {
|
|
77
|
|
- try
|
|
78
|
|
- {
|
|
|
69
|
+ public AjaxResult uploadFile(MultipartFile file) throws Exception {
|
|
|
70
|
+ try {
|
|
79
|
71
|
// 上传文件路径
|
|
80
|
72
|
String filePath = RuoYiConfig.getUploadPath();
|
|
81
|
73
|
// 上传并返回新文件名称
|
|
|
@@ -87,9 +79,7 @@ public class CommonController
|
|
87
|
79
|
ajax.put("newFileName", FileUtils.getName(fileName));
|
|
88
|
80
|
ajax.put("originalFilename", file.getOriginalFilename());
|
|
89
|
81
|
return ajax;
|
|
90
|
|
- }
|
|
91
|
|
- catch (Exception e)
|
|
92
|
|
- {
|
|
|
82
|
+ } catch (Exception e) {
|
|
93
|
83
|
return AjaxResult.error(e.getMessage());
|
|
94
|
84
|
}
|
|
95
|
85
|
}
|
|
|
@@ -98,18 +88,15 @@ public class CommonController
|
|
98
|
88
|
* 通用上传请求(多个)
|
|
99
|
89
|
*/
|
|
100
|
90
|
@PostMapping("/uploads")
|
|
101
|
|
- public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception
|
|
102
|
|
- {
|
|
103
|
|
- try
|
|
104
|
|
- {
|
|
|
91
|
+ public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception {
|
|
|
92
|
+ try {
|
|
105
|
93
|
// 上传文件路径
|
|
106
|
94
|
String filePath = RuoYiConfig.getUploadPath();
|
|
107
|
95
|
List<String> urls = new ArrayList<String>();
|
|
108
|
96
|
List<String> fileNames = new ArrayList<String>();
|
|
109
|
97
|
List<String> newFileNames = new ArrayList<String>();
|
|
110
|
98
|
List<String> originalFilenames = new ArrayList<String>();
|
|
111
|
|
- for (MultipartFile file : files)
|
|
112
|
|
- {
|
|
|
99
|
+ for (MultipartFile file : files) {
|
|
113
|
100
|
// 上传并返回新文件名称
|
|
114
|
101
|
String fileName = FileUploadUtils.upload(filePath, file);
|
|
115
|
102
|
String url = serverConfig.getUrl() + fileName;
|
|
|
@@ -124,9 +111,7 @@ public class CommonController
|
|
124
|
111
|
ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
|
|
125
|
112
|
ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
|
|
126
|
113
|
return ajax;
|
|
127
|
|
- }
|
|
128
|
|
- catch (Exception e)
|
|
129
|
|
- {
|
|
|
114
|
+ } catch (Exception e) {
|
|
130
|
115
|
return AjaxResult.error(e.getMessage());
|
|
131
|
116
|
}
|
|
132
|
117
|
}
|
|
|
@@ -136,12 +121,9 @@ public class CommonController
|
|
136
|
121
|
*/
|
|
137
|
122
|
@GetMapping("/download/resource")
|
|
138
|
123
|
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
|
|
139
|
|
- throws Exception
|
|
140
|
|
- {
|
|
141
|
|
- try
|
|
142
|
|
- {
|
|
143
|
|
- if (!FileUtils.checkAllowDownload(resource))
|
|
144
|
|
- {
|
|
|
124
|
+ throws Exception {
|
|
|
125
|
+ try {
|
|
|
126
|
+ if (!FileUtils.checkAllowDownload(resource)) {
|
|
145
|
127
|
throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
|
|
146
|
128
|
}
|
|
147
|
129
|
// 本地资源路径
|
|
|
@@ -153,9 +135,7 @@ public class CommonController
|
|
153
|
135
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
|
154
|
136
|
FileUtils.setAttachmentResponseHeader(response, downloadName);
|
|
155
|
137
|
FileUtils.writeBytes(downloadPath, response.getOutputStream());
|
|
156
|
|
- }
|
|
157
|
|
- catch (Exception e)
|
|
158
|
|
- {
|
|
|
138
|
+ } catch (Exception e) {
|
|
159
|
139
|
log.error("下载文件失败", e);
|
|
160
|
140
|
}
|
|
161
|
141
|
}
|