diff --git a/upload-file-backend/src/main/java/cn/czh/advice/GlobalExceptionHandler.java b/upload-file-backend/src/main/java/cn/czh/advice/GlobalExceptionHandler.java index 84ba312..febe9ab 100644 --- a/upload-file-backend/src/main/java/cn/czh/advice/GlobalExceptionHandler.java +++ b/upload-file-backend/src/main/java/cn/czh/advice/GlobalExceptionHandler.java @@ -24,4 +24,10 @@ public class GlobalExceptionHandler { log.error("业务异常: {}", e.getMessage(), e); return Result.error(500, e.getMessage()); } + + @ExceptionHandler(Exception.class) + public Result handleException(Exception e) { + log.error("服务器异常: {}", e.getMessage(), e); + return Result.error(500, "服务器异常"); + } } \ No newline at end of file diff --git a/upload-file-backend/src/main/java/cn/czh/controller/LocalFileController.java b/upload-file-backend/src/main/java/cn/czh/controller/LocalFileController.java index 62c6c35..21c22ee 100644 --- a/upload-file-backend/src/main/java/cn/czh/controller/LocalFileController.java +++ b/upload-file-backend/src/main/java/cn/czh/controller/LocalFileController.java @@ -1,6 +1,7 @@ package cn.czh.controller; import cn.czh.base.Result; +import cn.czh.dto.req.RenameFile; import cn.czh.service.ILocalFileService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; @@ -74,6 +75,9 @@ public class LocalFileController { return Result.success(localFileService.getFileTree(path, showFiles, showFolders, maxDepth)); } + /** + * 文件加密 + */ @PostMapping("/encrypt") public Result encryptFile(@RequestParam String filePath, @RequestParam String password) { File file = new File(filePath); @@ -86,6 +90,9 @@ public class LocalFileController { return Result.success(localFileService.encryptFile(filePath, password)); } + /** + * 文件解密 + */ @PostMapping("/decrypt") public Result decryptFile(@RequestParam String filePath, @RequestParam String password) { File file = new File(filePath); @@ -97,4 +104,13 @@ public class LocalFileController { } return Result.success(localFileService.decryptFile(filePath, password)); } + + /** + * 批量修改文件名 + */ + @PostMapping("/batchRenameFile") + public Result batchRenameFile(@RequestBody List fileRenames) { + localFileService.batchRenameFile(fileRenames); + return Result.success(); + } } diff --git a/upload-file-backend/src/main/java/cn/czh/dto/req/RenameFile.java b/upload-file-backend/src/main/java/cn/czh/dto/req/RenameFile.java new file mode 100644 index 0000000..9b3c260 --- /dev/null +++ b/upload-file-backend/src/main/java/cn/czh/dto/req/RenameFile.java @@ -0,0 +1,12 @@ +package cn.czh.dto.req; + +import lombok.Data; + +@Data +public class RenameFile { + + private String absolutePath; + + private String newName; + +} diff --git a/upload-file-backend/src/main/java/cn/czh/service/ILocalFileService.java b/upload-file-backend/src/main/java/cn/czh/service/ILocalFileService.java index b019d2c..25d915a 100644 --- a/upload-file-backend/src/main/java/cn/czh/service/ILocalFileService.java +++ b/upload-file-backend/src/main/java/cn/czh/service/ILocalFileService.java @@ -2,6 +2,7 @@ package cn.czh.service; import cn.czh.dto.FileNode; import cn.czh.dto.IndexResult; +import cn.czh.dto.req.RenameFile; import java.io.IOException; import java.util.List; @@ -19,4 +20,7 @@ public interface ILocalFileService { String encryptFile(String filePath, String password); String decryptFile(String filePath, String password); + + void batchRenameFile(List fileRenames); + } diff --git a/upload-file-backend/src/main/java/cn/czh/service/impl/LocalFileServiceImpl.java b/upload-file-backend/src/main/java/cn/czh/service/impl/LocalFileServiceImpl.java index d2f4741..148e056 100644 --- a/upload-file-backend/src/main/java/cn/czh/service/impl/LocalFileServiceImpl.java +++ b/upload-file-backend/src/main/java/cn/czh/service/impl/LocalFileServiceImpl.java @@ -3,9 +3,11 @@ package cn.czh.service.impl; import cn.czh.base.BusinessException; import cn.czh.dto.FileNode; import cn.czh.dto.IndexResult; +import cn.czh.dto.req.RenameFile; import cn.czh.service.ILocalFileService; import cn.czh.utils.FileSearchUtil; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.compress.utils.Lists; import org.springframework.stereotype.Service; import javax.annotation.PreDestroy; @@ -250,6 +252,65 @@ public class LocalFileServiceImpl implements ILocalFileService { } } + @Override + public void batchRenameFile(List fileRenames) { + if (fileRenames == null || fileRenames.isEmpty()) { + throw new BusinessException("文件重命名列表为空"); + } + + List successfullyRenamed = Lists.newArrayList(); + List originalPaths = Lists.newArrayList(); + + try { + for (RenameFile fileRename : fileRenames) { + File oldFile = new File(fileRename.getAbsolutePath()); + + if (!oldFile.exists()) { + throw new BusinessException("文件未找到: " + fileRename.getAbsolutePath()); + } + + String parentPath = oldFile.getParent(); + File newFile = new File(parentPath + File.separator + fileRename.getNewName()); + + // 执行重命名 + if (oldFile.renameTo(newFile)) { + successfullyRenamed.add(newFile); + originalPaths.add(fileRename.getAbsolutePath()); + } else { + throw new BusinessException("文件重命名失败: " + fileRename.getAbsolutePath()); + } + } + } catch (Exception e) { + rollbackRenamedFiles(successfullyRenamed, originalPaths); + throw e; + } + } + + /** + * 回滚已重命名的文件 + * @param renamedFiles 已重命名的文件列表 + * @param originalPaths 原始文件路径列表 + */ + private void rollbackRenamedFiles(List renamedFiles, List originalPaths) { + if (renamedFiles.isEmpty()) { + return; + } + + for (int i = 0; i < renamedFiles.size(); i++) { + File renamedFile = renamedFiles.get(i); + File originalFile = new File(originalPaths.get(i)); + try { + if (renamedFile.exists()) { + if (!renamedFile.renameTo(originalFile)) { + log.error("回滚失败: {}", renamedFile.getAbsolutePath()); + } + } + } catch (Exception ex) { + log.error("回滚失败: {}", ex.getMessage(), ex); + } + } + } + private SecretKey generateKey(String password, byte[] salt) throws Exception { PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); diff --git a/upload-file-frontend/src/components/Toolbox/tools/BatchRename.vue b/upload-file-frontend/src/components/Toolbox/tools/BatchRename.vue index fb8dfe2..76d57b5 100644 --- a/upload-file-frontend/src/components/Toolbox/tools/BatchRename.vue +++ b/upload-file-frontend/src/components/Toolbox/tools/BatchRename.vue @@ -5,16 +5,33 @@ - + + + + + + + + + + + + + + + +
- 已选择的文件 + 文件重命名预览 全部清除
- - + + + +