xm
2024-06-14 722af26bc6fec32bb289b1df51a9016a4935610f
提交 | 用户 | 时间
722af2 1 package com.dl.web.controller.common;
X 2
3 import com.dl.common.config.DLConfig;
4 import com.dl.common.constant.Constants;
5 import com.dl.common.utils.FileUtils;
6 import com.dl.common.utils.StringUtils;
7 import com.dl.common.utils.file.FileUploadUtils;
8 import com.dl.framework.config.ServerConfig;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.beans.factory.annotation.Value;
13 import org.springframework.http.MediaType;
14 import org.springframework.web.bind.annotation.GetMapping;
15 import org.springframework.web.bind.annotation.PostMapping;
16 import org.springframework.web.bind.annotation.RequestMapping;
17 import org.springframework.web.bind.annotation.RestController;
18 import org.springframework.web.multipart.MultipartFile;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 /**
29  * 通用请求处理
30  *
31  * @author dl
32  */
33 @RestController
34 @RequestMapping("/common")
35 public class CommonController {
36     private static final Logger log = LoggerFactory.getLogger(CommonController.class);
37
38     @Autowired
39     private ServerConfig serverConfig;
40
41     private static final String FILE_DELIMETER = ",";
42
43     @Value("${dl.profileView}")
44     private String uploadPath;
45
46     /**
47      * 通用下载请求
48      *
49      * @param fileName 文件名称
50      * @param delete   是否删除
51      */
52     @GetMapping("/download")
53     public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
54         try {
55             if (!FileUtils.checkAllowDownload(fileName)) {
56                 throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
57             }
58             String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
59             String filePath = DLConfig.getUploadPath() + fileName;
60
61             response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
62             FileUtils.setAttachmentResponseHeader(response, realFileName);
63             FileUtils.writeBytes(filePath, response.getOutputStream());
64             if (delete) {
65                 FileUtils.deleteFile(filePath);
66             }
67         } catch (Exception e) {
68             log.error("下载文件失败", e);
69         }
70     }
71
72     /**
73      * 通用上传请求(单个)
74      */
75     @PostMapping("/upload")
76     public Map<String, Object> uploadFile(MultipartFile file) throws IOException {
77         // 上传文件路径
78         String filePath = DLConfig.getUploadPath();
79         // 上传并返回新文件名称
80         String fileName = FileUploadUtils.upload(filePath, file);
81         String url = serverConfig.getUrl() + fileName;
82         Map<String, Object> ajax = new HashMap<>();
83         ajax.put("url", fileName);
84         ajax.put("viewUrl",uploadPath+ fileName);
85         ajax.put("name",FileUtils.getName(fileName));//file.getOriginalFilename()
86         ajax.put("newFileName", FileUtils.getName(fileName));
87         ajax.put("originalFilename", file.getOriginalFilename());
88         ajax.put("size", file.getSize());
89         return ajax;
90
91
92     }
93
94     /**
95      * 通用上传请求(多个)
96      */
97     @PostMapping("/uploads")
98     public Map<String, Object> uploadFiles(List<MultipartFile> files) throws IOException {
99         // 上传文件路径
100         String filePath = DLConfig.getUploadPath();
101         List<String> urls = new ArrayList<String>();
102         List<String> fileNames = new ArrayList<String>();
103         List<String> newFileNames = new ArrayList<String>();
104         List<String> originalFilenames = new ArrayList<String>();
105         for (MultipartFile file : files) {
106             // 上传并返回新文件名称
107             String fileName = FileUploadUtils.upload(filePath, file);
108             String url = serverConfig.getUrl() + fileName;
109             urls.add(url);
110             fileNames.add(fileName);
111             newFileNames.add(FileUtils.getName(fileName));
112             originalFilenames.add(file.getOriginalFilename());
113         }
114         Map<String, Object> ajax = new HashMap<>();
115         ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
116         ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
117         ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
118         ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
119         return ajax;
120
121     }
122
123     /**
124      * 本地资源通用下载
125      */
126     @GetMapping("/download/resource")
127     public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
128         throws Exception {
129         try {
130             if (!FileUtils.checkAllowDownload(resource)) {
131                 throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
132             }
133             // 本地资源路径
134             String localPath = DLConfig.getProfile();
135             // 数据库资源地址
136             String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
137             // 下载名称
138             String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
139             response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
140             FileUtils.setAttachmentResponseHeader(response, downloadName);
141             FileUtils.writeBytes(downloadPath, response.getOutputStream());
142         } catch (Exception e) {
143             log.error("下载文件失败", e);
144         }
145     }
146 }