From d7066714e57308bbfa6ff66a8a916de2985abdeb Mon Sep 17 00:00:00 2001 From: ybb <2532447764@qq.com> Date: Wed, 6 Aug 2025 08:48:15 +0800 Subject: [PATCH] =?UTF-8?q?8/6,ybb=E6=96=87=E4=BB=B6=E6=A8=A1=E7=B3=8A?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../znpt/mapper/BusinessDataFileMapper.java | 3 + .../znpt/service/BusinessDataFileService.java | 11 ++- .../impl/BusinessDataFileServiceImpl.java | 71 ++++++++++++++++++- .../mapper/BusinessDataFileMapper.xml | 9 +++ .../BusinessDataFileController.java | 9 ++- 5 files changed, 98 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/dite/znpt/mapper/BusinessDataFileMapper.java b/core/src/main/java/com/dite/znpt/mapper/BusinessDataFileMapper.java index e35c179..0420931 100644 --- a/core/src/main/java/com/dite/znpt/mapper/BusinessDataFileMapper.java +++ b/core/src/main/java/com/dite/znpt/mapper/BusinessDataFileMapper.java @@ -17,4 +17,7 @@ public interface BusinessDataFileMapper { void add(BusinessDataFileEntity businessDataFileEntity); String getPath(Long fileId); + + // 在接口中添加重命名方法 + void reName(@Param("fileId") Long fileId, @Param("newFileName") String newFileName, @Param("newFilePath") String newFilePath); } diff --git a/core/src/main/java/com/dite/znpt/service/BusinessDataFileService.java b/core/src/main/java/com/dite/znpt/service/BusinessDataFileService.java index 2edabea..031a4c3 100644 --- a/core/src/main/java/com/dite/znpt/service/BusinessDataFileService.java +++ b/core/src/main/java/com/dite/znpt/service/BusinessDataFileService.java @@ -4,10 +4,13 @@ import com.dite.znpt.domain.Result; import com.dite.znpt.domain.entity.BusinessDataFileEntity; import com.dite.znpt.domain.page.PageBean; import io.swagger.annotations.ApiOperation; +import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestParam; -@ApiOperation("商务资料文件service") +import javax.servlet.http.HttpServletResponse; +@ApiOperation("商务资料文件service") +@Service public interface BusinessDataFileService { PageBean pageSelect(Integer page, Integer pageSize, Long folderId, String fileName); @@ -17,4 +20,10 @@ public interface BusinessDataFileService { void add(BusinessDataFileEntity businessDataFileEntity); String getPath(Long fileId); + + // 在接口中添加重命名方法 + Result reName(Long fileId, String newFileName); + + // 在接口中添加预览方法 +// Result preview(Long fileId, HttpServletResponse response); } diff --git a/core/src/main/java/com/dite/znpt/service/impl/BusinessDataFileServiceImpl.java b/core/src/main/java/com/dite/znpt/service/impl/BusinessDataFileServiceImpl.java index 5f6198b..c138abf 100644 --- a/core/src/main/java/com/dite/znpt/service/impl/BusinessDataFileServiceImpl.java +++ b/core/src/main/java/com/dite/znpt/service/impl/BusinessDataFileServiceImpl.java @@ -87,8 +87,77 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService { @ApiOperation("获取文件路径") public String getPath(Long fileId) { - System.out.println(fileId+" 77777777777impl777777777777777"); return businessDataFileMapper.getPath(fileId); } + @ApiOperation("重命名文件") + @Override + public Result reName(Long fileId, String newFileName) { + // 参数校验 + if (fileId == null) { + return Result.error("文件ID不能为空"); + } + if (newFileName == null || newFileName.trim().isEmpty()) { + return Result.error("新文件名不能为空"); + } + if (newFileName.length() > 100) { + return Result.error("文件名过长"); + } + + try { + // 获取原文件信息 + String oldFilePath = businessDataFileMapper.getPath(fileId); + if (oldFilePath == null) { + return Result.error("文件不存在"); + } + + // 创建原文件对象 + File oldFile = new File(oldFilePath); + if (!oldFile.exists()) { + return Result.error("文件不存在"); + } + + // 构建新文件路径 + String parentPath = oldFile.getParent(); + String fileExtension = ""; + String fileNameWithoutExt = newFileName; + + // 获取原文件扩展名 + int lastDotIndex = oldFile.getName().lastIndexOf('.'); + if (lastDotIndex > 0) { + fileExtension = oldFile.getName().substring(lastDotIndex); + } + + // 如果新文件名没有扩展名,则添加原文件扩展名 + if (!newFileName.contains(".")) { + newFileName = newFileName + fileExtension; + } + + // 构建新文件路径 + String newFilePath = parentPath + File.separator + newFileName; + File newFile = new File(newFilePath); + + // 检查新文件名是否已存在 + if (newFile.exists()) { + return Result.error("文件名已存在"); + } + + // 重命名实际文件 + boolean renameSuccess = oldFile.renameTo(newFile); + if (!renameSuccess) { + return Result.error("文件重命名失败"); + } + + // 更新数据库中的文件信息 + businessDataFileMapper.reName(fileId, newFileName, newFilePath); + + return Result.okM("文件重命名成功"); + + } catch (Exception e) { + return Result.error("文件重命名失败: " + e.getMessage()); + } + } + + + } diff --git a/core/src/main/resources/mapper/BusinessDataFileMapper.xml b/core/src/main/resources/mapper/BusinessDataFileMapper.xml index f7f3b44..2d3be14 100644 --- a/core/src/main/resources/mapper/BusinessDataFileMapper.xml +++ b/core/src/main/resources/mapper/BusinessDataFileMapper.xml @@ -37,5 +37,14 @@ + + + update business_data_part_file + + file_name = #{newFileName}, + file_path = #{newFilePath}, + + where file_id = #{fileId} + diff --git a/web/src/main/java/com/dite/znpt/web/controller/BusinessDataFileController.java b/web/src/main/java/com/dite/znpt/web/controller/BusinessDataFileController.java index 09df174..6e1acb4 100644 --- a/web/src/main/java/com/dite/znpt/web/controller/BusinessDataFileController.java +++ b/web/src/main/java/com/dite/znpt/web/controller/BusinessDataFileController.java @@ -20,8 +20,6 @@ import java.time.LocalDateTime; import java.util.Date; import java.util.List; import java.util.UUID; - -import com.dite.znpt.domain.entity.BusinessDataFileEntity; import org.springframework.web.multipart.MultipartFile; /* @@ -55,7 +53,6 @@ public class BusinessDataFileController { @PostMapping("/add") public Result add(@RequestParam("file") MultipartFile file, @RequestParam Long folderId) { - System.out.println(file + " 77777777777777777777777777" + folderId); if (file.isEmpty()) { return Result.error("上传文件为空"); @@ -135,4 +132,10 @@ public class BusinessDataFileController { } } + @ApiOperation(value = "重命名文件", httpMethod = "PUT") + @PutMapping("/rename") + public Result reName(@RequestParam Long fileId, @RequestParam String newFileName) { + return businessDataFileService.reName(fileId, newFileName); + } + }