diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java index d58b1078..0c46bbe6 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java @@ -1,6 +1,9 @@ package com.ruoyi.web.controller.common; +import java.io.File; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -10,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @@ -160,4 +164,35 @@ public class CommonController log.error("下载文件失败", e); } } + //文件删除(多个) + @PostMapping("/delete/resource") + public boolean deleteFile(@RequestBody HashMap map) { + // 假设filePath是一个逗号分隔的字符串 + String filePathString = map.get("filePath").toString(); + // 本地资源路径 + String localPath = RuoYiConfig.getProfile(); + // 将filePathString按逗号分割成文件路径列表 + List filePaths = Arrays.asList(filePathString.split(",")); + // 遍历文件路径列表并尝试删除每个文件 + boolean allDeleted = true; // 假设所有文件都能被删除 + for (String filePath : filePaths) { + // 去除可能存在的空格和前后逗号 + filePath = filePath.trim(); + if (filePath.isEmpty()) { + continue; // 跳过空路径 + } + // 数据库资源地址 + String downloadPath = localPath + StringUtils.substringAfter(filePath, Constants.RESOURCE_PREFIX); + File file = new File(downloadPath); + // 路径为文件且不为空则进行删除 + if (file.isFile() && file.exists()) { + if (!file.delete()) { + allDeleted = false; // 如果有文件删除失败,则标记为false + } + } else { + allDeleted = false; // 如果文件不存在或不是文件,也标记为false + } + } + return allDeleted; // 返回所有文件是否都被成功删除 + } }