提交 | 用户 | 时间
|
fd2207
|
1 |
package com.jcdm.web.controller.common; |
懒 |
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.List; |
|
5 |
import javax.servlet.http.HttpServletRequest; |
|
6 |
import javax.servlet.http.HttpServletResponse; |
|
7 |
import org.slf4j.Logger; |
|
8 |
import org.slf4j.LoggerFactory; |
|
9 |
import org.springframework.beans.factory.annotation.Autowired; |
|
10 |
import org.springframework.http.MediaType; |
|
11 |
import org.springframework.web.bind.annotation.GetMapping; |
|
12 |
import org.springframework.web.bind.annotation.PostMapping; |
|
13 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
14 |
import org.springframework.web.bind.annotation.RestController; |
|
15 |
import org.springframework.web.multipart.MultipartFile; |
|
16 |
import com.jcdm.common.config.MesConfig; |
|
17 |
import com.jcdm.common.constant.Constants; |
|
18 |
import com.jcdm.common.core.domain.AjaxResult; |
|
19 |
import com.jcdm.common.utils.StringUtils; |
|
20 |
import com.jcdm.common.utils.file.FileUploadUtils; |
|
21 |
import com.jcdm.common.utils.file.FileUtils; |
|
22 |
import com.jcdm.framework.config.ServerConfig; |
|
23 |
|
|
24 |
/** |
|
25 |
* 通用请求处理 |
|
26 |
* |
|
27 |
* @author jc |
|
28 |
*/ |
|
29 |
@RestController |
|
30 |
@RequestMapping("/common") |
|
31 |
public class CommonController |
|
32 |
{ |
|
33 |
private static final Logger log = LoggerFactory.getLogger(CommonController.class); |
|
34 |
|
|
35 |
@Autowired |
|
36 |
private ServerConfig serverConfig; |
|
37 |
|
|
38 |
private static final String FILE_DELIMETER = ","; |
|
39 |
|
|
40 |
/** |
|
41 |
* 通用下载请求 |
|
42 |
* |
|
43 |
* @param fileName 文件名称 |
|
44 |
* @param delete 是否删除 |
|
45 |
*/ |
|
46 |
@GetMapping("/download") |
|
47 |
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) |
|
48 |
{ |
|
49 |
try |
|
50 |
{ |
|
51 |
if (!FileUtils.checkAllowDownload(fileName)) |
|
52 |
{ |
|
53 |
throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName)); |
|
54 |
} |
|
55 |
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1); |
|
56 |
String filePath = MesConfig.getDownloadPath() + fileName; |
|
57 |
|
|
58 |
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); |
|
59 |
FileUtils.setAttachmentResponseHeader(response, realFileName); |
|
60 |
FileUtils.writeBytes(filePath, response.getOutputStream()); |
|
61 |
if (delete) |
|
62 |
{ |
|
63 |
FileUtils.deleteFile(filePath); |
|
64 |
} |
|
65 |
} |
|
66 |
catch (Exception e) |
|
67 |
{ |
|
68 |
log.error("下载文件失败", e); |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* 通用上传请求(单个) |
|
74 |
*/ |
|
75 |
@PostMapping("/upload") |
|
76 |
public AjaxResult uploadFile(MultipartFile file) throws Exception |
|
77 |
{ |
|
78 |
try |
|
79 |
{ |
|
80 |
// 上传文件路径 |
|
81 |
String filePath = MesConfig.getUploadPath(); |
|
82 |
// 上传并返回新文件名称 |
|
83 |
String fileName = FileUploadUtils.upload(filePath, file); |
|
84 |
String url = serverConfig.getUrl() + fileName; |
|
85 |
AjaxResult ajax = AjaxResult.success(); |
|
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 = MesConfig.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)); |
|
127 |
return ajax; |
|
128 |
} |
|
129 |
catch (Exception e) |
|
130 |
{ |
|
131 |
return AjaxResult.error(e.getMessage()); |
|
132 |
} |
|
133 |
} |
|
134 |
|
|
135 |
/** |
|
136 |
* 本地资源通用下载 |
|
137 |
*/ |
|
138 |
@GetMapping("/download/resource") |
|
139 |
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response) |
|
140 |
throws Exception |
|
141 |
{ |
|
142 |
try |
|
143 |
{ |
|
144 |
if (!FileUtils.checkAllowDownload(resource)) |
|
145 |
{ |
|
146 |
throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource)); |
|
147 |
} |
|
148 |
// 本地资源路径 |
|
149 |
String localPath = MesConfig.getProfile(); |
|
150 |
// 数据库资源地址 |
|
151 |
String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX); |
|
152 |
// 下载名称 |
|
153 |
String downloadName = StringUtils.substringAfterLast(downloadPath, "/"); |
|
154 |
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); |
|
155 |
FileUtils.setAttachmentResponseHeader(response, downloadName); |
|
156 |
FileUtils.writeBytes(downloadPath, response.getOutputStream()); |
|
157 |
} |
|
158 |
catch (Exception e) |
|
159 |
{ |
|
160 |
log.error("下载文件失败", e); |
|
161 |
} |
|
162 |
} |
|
163 |
} |