Просмотр исходного кода

Merge remote-tracking branch 'origin/master'

wangxx недель назад: 2
Родитель
Сommit
6de750694a

+ 36 - 40
airport-admin/src/main/java/com/sundot/airport/web/controller/common/CommonController.java

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