From 87c2736894b0423051952a09c22af5fa49e79cec Mon Sep 17 00:00:00 2001 From: wangsixiang <2970484253@qq.com> Date: Wed, 12 Jun 2024 14:35:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A8=E9=80=81=E4=BF=AE=E6=94=B9,=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=8F=91=E5=B8=83=E8=AE=B0=E5=BD=95=E7=AE=A1=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E5=8F=91=E5=B8=83=E6=B8=A0=E9=81=93=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/common/CommonController.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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; // 返回所有文件是否都被成功删除 + } }