8月13(包括加班),修改了重命名bug.还对路径格式,对于不同系统做了适配,兼容性更好。

This commit is contained in:
ybb 2025-08-13 19:48:38 +08:00
parent f7b877aef8
commit 2a72604dde
8 changed files with 116 additions and 13 deletions

View File

@ -36,4 +36,15 @@ public interface BusinessDataFileMapper {
@Param("newFileName") String newFileName,
@Param("newFilePath") String newFilePath);
// // 批量更新文件路径
// void updateFilePathByFolderId(
// @Param("folderId") Long folderId,
// @Param("newFolderPath") String newFolderPath,
// @Param("separator") String separator);
// 批量更新子文件夹下文件的路径
void updateSubFilePaths(@Param("oldParentPath1") String oldParentPath1,
@Param("newParentPath1") String newParentPath1,
@Param("oldParentPath2") String oldParentPath2
);
}

View File

@ -29,4 +29,12 @@ public interface BusinessDataMapper {
void reName(BusinessDataEntity businessDataEntity);
public List<BusinessDataEntity> ListWithCondition(@Param("folderName") String folderName);
// 批量更新子文件夹路径
void updateSubFolderPaths(@Param("oldParentPath1") String oldParentPath,
@Param("newParentPath1") String newParentPath,
@Param("processedOldParentPath") String processedOldParentPath,
@Param("processedNewParentPath") String processedNewParentPath,
@Param("folderId") Long folderId
);
}

View File

@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
@ApiOperation("商务资料文件service")
@Service
@ -40,4 +41,11 @@ public interface BusinessDataFileService {
@ApiOperation("预览文件")
void preview(Long fileId, HttpServletResponse response);
// @ApiOperation("批量更新文件路径")
// public void updateFilePathByFolderId(Long folderId, String newFolderPath);
@ApiOperation("批量更新子文件夹下文件的路径")
void updateSubFilePaths(String oldParentPath, String newParentPath);
}

View File

@ -15,4 +15,6 @@ public interface BusinessDataService {
Result delete(Long folderId);
Result reName(Long folderId, String newName);
void updateSubFolderPaths(String oldParentPath1, String newParentPath1, Long folderId);
}

View File

@ -81,7 +81,8 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
}
@ApiOperation("删除文件")
public Result delete(Long fileId, Long folderId) {
//删除数据库数据
//删除文件夹时候调用这个方法才会删除所有文件的数据库数据
// 至于具体文件不用在这个方法删除
if (folderId != null){
businessDataFileMapper.delete(null,folderId);
return Result.okM("删除成功");
@ -155,8 +156,11 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
}
// 构建新文件路径
// 获取父目录
String parentPath = oldFile.getParent();
// 获取文件扩展名
String fileExtension = "";
// 获取文件名不包含扩展名
String fileNameWithoutExt = newFileName;
// 获取原文件扩展名
@ -246,7 +250,7 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
byte[] bytes = file.getBytes();
String uploadDir = businessDataService.getPath(folderId);
File uploadedFile = new File(uploadDir + "/" + file.getOriginalFilename());
File uploadedFile = new File(uploadDir + File.separator + file.getOriginalFilename());
if (uploadedFile.exists()) {
return Result.error("文件已存在");
}
@ -256,7 +260,7 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
BusinessDataFileEntity fileEntity = new BusinessDataFileEntity();
fileEntity.setFolderId(folderId);
fileEntity.setFileName(file.getOriginalFilename());
fileEntity.setFilePath(uploadDir + "/" + file.getOriginalFilename());
fileEntity.setFilePath(uploadDir + File.separator + file.getOriginalFilename());
fileEntity.setFileType(file.getContentType());
fileEntity.setFileSize(file.getSize()/1024);
fileEntity.setUploadTime(new Date());
@ -544,4 +548,21 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
}
}
// @ApiOperation("批量更新文件路径")
// @Override
// public void updateFilePathByFolderId(Long folderId, String newFolderPath) {
// businessDataFileMapper.updateFilePathByFolderId(folderId, newFolderPath, File.separator);
// }
@ApiOperation("批量更新子文件夹下文件的路径")
@Override
public void updateSubFilePaths(String oldParentPath1, String newParentPath1) {
// 处理路径中的分隔符如果是反斜杠则替换为双反斜杠
String oldParentPath2 = oldParentPath1;
if ("\\".equals(File.separator)) {
oldParentPath2 = oldParentPath1.replace("\\", "\\\\");
}
businessDataFileMapper.updateSubFilePaths(oldParentPath1, newParentPath1, oldParentPath2);
}
}

View File

@ -70,7 +70,7 @@ public class BusinessDataServiceImpl implements BusinessDataService {
}
// 文件夹名称前置一个/
String folderName1 = "/" + folderName;
String folderName1 = File.separator + folderName;
// 目标文件夹
File targetDir = Paths.get(businessDataPath, folderName1).toFile();
if (parentId != 0L) {
@ -147,23 +147,37 @@ public class BusinessDataServiceImpl implements BusinessDataService {
}
}
@ApiOperation("批量更新子文件夹路径")
@Override
public void updateSubFolderPaths(String oldParentPath1, String newParentPath1,Long folderId) {
// 处理路径中的分隔符如果是反斜杠则替换为双反斜杠
String processedOldParentPath = oldParentPath1;
String processedNewParentPath = newParentPath1;
if ("\\".equals(File.separator)) {
processedOldParentPath = oldParentPath1.replace("\\", "\\\\");
processedNewParentPath = newParentPath1.replace("\\", "\\\\");
}
businessDataMapper.updateSubFolderPaths(oldParentPath1,newParentPath1,processedOldParentPath, processedNewParentPath, folderId);
}
@ApiOperation("重命名文件夹")
@Override
public Result reName(Long folderId, String newName) {
// 获取文件夹路径
String folderPath = businessDataMapper.getPath(folderId);
String newPath = folderPath.substring(0, folderPath.lastIndexOf('\\')) + "\\" + newName;
//
// //想命名的原文件的路径
// File file = new File("f:/a/a.xlsx");
// //将原文件更改为f:\a\b.xlsx其中路径是必要的注意
// file.renameTo(new File("f:/a/b.xlsx"));
// 想命名的原文件夹的路径
String newPath = folderPath.substring(0, folderPath.lastIndexOf(File.separator)) + File.separator + newName;
// 重命名物理文件夹
File file1 = new File(folderPath);
// 将原文件夹更改为A其中路径是必要的注意
file1.renameTo(new File(newPath));
boolean renameSuccess = file1.renameTo(new File(newPath));
if (!renameSuccess) {
return Result.error("文件夹重命名失败");
}
LocalDateTime now = LocalDateTime.now();
// 更新文件夹信息
BusinessDataEntity businessDataEntity = new BusinessDataEntity(
folderId,
newName,
@ -174,6 +188,21 @@ public class BusinessDataServiceImpl implements BusinessDataService {
null,
newPath);
businessDataMapper.reName(businessDataEntity);
// // 批量更新该文件夹下所有文件的路径
// businessDataFileService.updateFilePathByFolderId(folderId, newPath);
String folderPath1 = folderPath+File.separator;
String newPath1 = newPath+File.separator;
// 批量更新子文件夹下所有文件的路径
businessDataFileService.updateSubFilePaths(folderPath1, newPath1);
// 批量更新子文件夹的路径
updateSubFolderPaths(folderPath1, newPath1,folderId);
return Result.okM("重命名成功");
}
}

View File

@ -94,5 +94,20 @@
</set>
where file_id = #{fileId}
</update>
<!-- &lt;!&ndash; 批量更新文件路径 &ndash;&gt;-->
<!-- <update id="updateFilePathByFolderId">-->
<!-- update business_data_part_file-->
<!-- set file_path = concat(#{newFolderPath}, #{separator}, file_name)-->
<!-- where folder_id = #{folderId}-->
<!-- </update>-->
<!-- 批量更新子文件夹下文件的路径 -->
<update id="updateSubFilePaths">
update business_data_part_file
set file_path = replace(file_path, #{oldParentPath1}, #{newParentPath1})
where file_path like concat(#{oldParentPath2}, '%')
</update>
</mapper>

View File

@ -51,5 +51,14 @@
</if>
</where>
</select>
<!-- 批量更新子文件夹路径 -->
<update id="updateSubFolderPaths">
update business_data_part
set folder_path = replace(folder_path, #{oldParentPath1}, #{newParentPath1}),
update_time = now()
where folder_path like concat(#{processedOldParentPath}, '%')
</update>
</mapper>