Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
1c136ed302
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -37,5 +37,14 @@
|
|||
</if>
|
||||
</where>
|
||||
</select>
|
||||
<!-- 在mapper中添加重命名SQL -->
|
||||
<update id="reName">
|
||||
update business_data_part_file
|
||||
<set>
|
||||
<if test="newFileName != null">file_name = #{newFileName},</if>
|
||||
<if test="newFilePath != null">file_path = #{newFilePath},</if>
|
||||
</set>
|
||||
where file_id = #{fileId}
|
||||
</update>
|
||||
</mapper>
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue