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