作者 RuoYi

组件ImageUpload支持多图同时选择上传

1 package com.ruoyi.web.controller.common; 1 package com.ruoyi.web.controller.common;
2 2
  3 +import java.util.ArrayList;
  4 +import java.util.List;
3 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.http.HttpServletResponse; 6 import javax.servlet.http.HttpServletResponse;
5 import org.slf4j.Logger; 7 import org.slf4j.Logger;
@@ -8,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -8,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.http.MediaType; 10 import org.springframework.http.MediaType;
9 import org.springframework.web.bind.annotation.GetMapping; 11 import org.springframework.web.bind.annotation.GetMapping;
10 import org.springframework.web.bind.annotation.PostMapping; 12 import org.springframework.web.bind.annotation.PostMapping;
  13 +import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.RestController; 14 import org.springframework.web.bind.annotation.RestController;
12 import org.springframework.web.multipart.MultipartFile; 15 import org.springframework.web.multipart.MultipartFile;
13 import com.ruoyi.common.config.RuoYiConfig; 16 import com.ruoyi.common.config.RuoYiConfig;
@@ -24,6 +27,7 @@ import com.ruoyi.framework.config.ServerConfig; @@ -24,6 +27,7 @@ import com.ruoyi.framework.config.ServerConfig;
24 * @author ruoyi 27 * @author ruoyi
25 */ 28 */
26 @RestController 29 @RestController
  30 +@RequestMapping("/common")
27 public class CommonController 31 public class CommonController
28 { 32 {
29 private static final Logger log = LoggerFactory.getLogger(CommonController.class); 33 private static final Logger log = LoggerFactory.getLogger(CommonController.class);
@@ -31,13 +35,15 @@ public class CommonController @@ -31,13 +35,15 @@ public class CommonController
31 @Autowired 35 @Autowired
32 private ServerConfig serverConfig; 36 private ServerConfig serverConfig;
33 37
  38 + private static final String FILE_DELIMETER = ",";
  39 +
34 /** 40 /**
35 * 通用下载请求 41 * 通用下载请求
36 * 42 *
37 * @param fileName 文件名称 43 * @param fileName 文件名称
38 * @param delete 是否删除 44 * @param delete 是否删除
39 */ 45 */
40 - @GetMapping("common/download") 46 + @GetMapping("/download")
41 public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) 47 public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
42 { 48 {
43 try 49 try
@@ -64,9 +70,9 @@ public class CommonController @@ -64,9 +70,9 @@ public class CommonController
64 } 70 }
65 71
66 /** 72 /**
67 - * 通用上传请求 73 + * 通用上传请求(单个)
68 */ 74 */
69 - @PostMapping("/common/upload") 75 + @PostMapping("/upload")
70 public AjaxResult uploadFile(MultipartFile file) throws Exception 76 public AjaxResult uploadFile(MultipartFile file) throws Exception
71 { 77 {
72 try 78 try
@@ -77,8 +83,47 @@ public class CommonController @@ -77,8 +83,47 @@ public class CommonController
77 String fileName = FileUploadUtils.upload(filePath, file); 83 String fileName = FileUploadUtils.upload(filePath, file);
78 String url = serverConfig.getUrl() + fileName; 84 String url = serverConfig.getUrl() + fileName;
79 AjaxResult ajax = AjaxResult.success(); 85 AjaxResult ajax = AjaxResult.success();
80 - ajax.put("fileName", fileName);  
81 ajax.put("url", url); 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 = RuoYiConfig.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));
82 return ajax; 127 return ajax;
83 } 128 }
84 catch (Exception e) 129 catch (Exception e)
@@ -90,7 +135,7 @@ public class CommonController @@ -90,7 +135,7 @@ public class CommonController
90 /** 135 /**
91 * 本地资源通用下载 136 * 本地资源通用下载
92 */ 137 */
93 - @GetMapping("/common/download/resource") 138 + @GetMapping("/download/resource")
94 public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response) 139 public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
95 throws Exception 140 throws Exception
96 { 141 {
@@ -256,4 +256,22 @@ public class FileUtils @@ -256,4 +256,22 @@ public class FileUtils
256 } 256 }
257 return strFileExtendName; 257 return strFileExtendName;
258 } 258 }
  259 +
  260 + /**
  261 + * 获取名称
  262 + *
  263 + * @param fileName 路径名称
  264 + * @return 没有文件路径的名称
  265 + */
  266 + public static String getName(String fileName)
  267 + {
  268 + if (fileName == null)
  269 + {
  270 + return null;
  271 + }
  272 + int lastUnixPos = fileName.lastIndexOf('/');
  273 + int lastWindowsPos = fileName.lastIndexOf('\\');
  274 + int index = Math.max(lastUnixPos, lastWindowsPos);
  275 + return fileName.substring(index + 1);
  276 + }
259 } 277 }
@@ -125,10 +125,7 @@ public class ScheduleUtils @@ -125,10 +125,7 @@ public class ScheduleUtils
125 int count = StringUtils.countMatches(packageName, "."); 125 int count = StringUtils.countMatches(packageName, ".");
126 if (count > 1) 126 if (count > 1)
127 { 127 {
128 - if (!StringUtils.containsAnyIgnoreCase(invokeTarget, Constants.JOB_WHITELIST_STR))  
129 - {  
130 - return false;  
131 - } 128 + return StringUtils.containsAnyIgnoreCase(invokeTarget, Constants.JOB_WHITELIST_STR);
132 } 129 }
133 return true; 130 return true;
134 } 131 }
1 <template> 1 <template>
2 <div class="component-upload-image"> 2 <div class="component-upload-image">
3 <el-upload 3 <el-upload
  4 + multiple
4 :action="uploadImgUrl" 5 :action="uploadImgUrl"
5 list-type="picture-card" 6 list-type="picture-card"
6 :on-success="handleUploadSuccess" 7 :on-success="handleUploadSuccess"
@@ -70,6 +71,8 @@ export default { @@ -70,6 +71,8 @@ export default {
70 }, 71 },
71 data() { 72 data() {
72 return { 73 return {
  74 + number: 0,
  75 + uploadList: [],
73 dialogImageUrl: "", 76 dialogImageUrl: "",
74 dialogVisible: false, 77 dialogVisible: false,
75 hideUpload: false, 78 hideUpload: false,
@@ -124,9 +127,14 @@ export default { @@ -124,9 +127,14 @@ export default {
124 }, 127 },
125 // 上传成功回调 128 // 上传成功回调
126 handleUploadSuccess(res) { 129 handleUploadSuccess(res) {
127 - this.fileList.push({ name: res.fileName, url: res.fileName }); 130 + this.uploadList.push({ name: res.fileName, url: res.fileName });
  131 + if (this.uploadList.length === this.number) {
  132 + this.fileList = this.fileList.concat(this.uploadList);
  133 + this.uploadList = [];
  134 + this.number = 0;
128 this.$emit("input", this.listToString(this.fileList)); 135 this.$emit("input", this.listToString(this.fileList));
129 this.loading.close(); 136 this.loading.close();
  137 + }
130 }, 138 },
131 // 上传前loading加载 139 // 上传前loading加载
132 handleBeforeUpload(file) { 140 handleBeforeUpload(file) {
@@ -163,6 +171,7 @@ export default { @@ -163,6 +171,7 @@ export default {
163 text: "上传中", 171 text: "上传中",
164 background: "rgba(0, 0, 0, 0.7)", 172 background: "rgba(0, 0, 0, 0.7)",
165 }); 173 });
  174 + this.number++;
166 }, 175 },
167 // 文件个数超出 176 // 文件个数超出
168 handleExceed() { 177 handleExceed() {