修复文件改后缀的问题。

This commit is contained in:
chenxudong 2024-12-04 14:45:30 +08:00
parent 78b8207bb0
commit c35f4cfef6
3 changed files with 39 additions and 12 deletions

View File

@ -4,6 +4,7 @@
package com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.service.impl; package com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.service.impl;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil; import cn.hutool.core.util.ZipUtil;
import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES; import cn.hutool.crypto.symmetric.AES;
@ -29,6 +30,7 @@ import electromagnetic.data.framework.share.constants.ElectromagneticConstants;
import electromagnetic.data.framework.share.exception.LoggerConstant; import electromagnetic.data.framework.share.exception.LoggerConstant;
import electromagnetic.data.framework.share.id.IdWorker; import electromagnetic.data.framework.share.id.IdWorker;
import electromagnetic.data.framework.share.model.ElectromagneticResultUtil; import electromagnetic.data.framework.share.model.ElectromagneticResultUtil;
import electromagnetic.data.framework.share.util.EleCommonUtil;
import electromagnetic.data.framework.share.util.EleZipUtil; import electromagnetic.data.framework.share.util.EleZipUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -134,7 +136,8 @@ public class EDDataServiceImpl implements EDDataService {
categoryParent = categoryParentList.get(0); categoryParent = categoryParentList.get(0);
filePathOfFolder = categoryParent.getCategoryId() + FOLDER_NAME_SEPARATOR + categoryParent.getCategoryName(); filePathOfFolder = categoryParent.getCategoryId() + FOLDER_NAME_SEPARATOR + categoryParent.getCategoryName();
if(edDataInfoParent != null) { if(edDataInfoParent != null) {
filePathOfFolder = filePathOfFolder + File.separator + edDataInfoParent.getDataName(); String fileType = StrUtil.isEmpty(edDataInfoParent.getFileType()) ? "" : "." + edDataInfoParent.getFileType();
filePathOfFolder = filePathOfFolder + File.separator + edDataInfoParent.getDataName() + fileType;
} }
} }
@ -234,13 +237,14 @@ public class EDDataServiceImpl implements EDDataService {
EDDataInfo edDataInfo = edDataInfoList.get(0); EDDataInfo edDataInfo = edDataInfoList.get(0);
String filePathOfFolder = getFilePathOfFolder(edDataInfo.getCategoryId()); String filePathOfFolder = getFilePathOfFolder(edDataInfo.getCategoryId());
String fileStorageFullPath = dataStoragePath + filePathOfFolder + File.separator + edDataInfo.getDataName(); String fileType = StrUtil.isEmpty(edDataInfo.getFileType()) ? "" : "." + edDataInfo.getFileType();
String fileStorageFullPath = dataStoragePath + filePathOfFolder + File.separator + edDataInfo.getDataName() + fileType;
if (!FileUtil.exist(fileStorageFullPath)){ if (!FileUtil.exist(fileStorageFullPath)){
throw new Exception("文件不存在"); throw new Exception("文件不存在");
} }
String fileNameNew = parames.getName(); String fileNameNew = parames.getName() + fileType;
if(fileNameNew != null && fileNameNew != "" && !fileNameNew.equals(edDataInfo.getDataName())) { if(fileNameNew != null && fileNameNew != "" && !fileNameNew.equals(edDataInfo.getDataName())) {
FileUtil.rename(Paths.get(fileStorageFullPath) ,fileNameNew,true); FileUtil.rename(Paths.get(fileStorageFullPath) ,fileNameNew,true);
} }
@ -306,13 +310,9 @@ public class EDDataServiceImpl implements EDDataService {
// 获取文件名 // 获取文件名
String fileFullName = fileInput.getOriginalFilename(); String fileFullName = fileInput.getOriginalFilename();
// 获取文件类型 // 获取文件类型
String fileType = ""; String fileType = EleCommonUtil.getFileType(fileFullName);
// 获取文件名称 // 获取文件名称
String fileName = ""; String fileName = EleCommonUtil.getFileName(fileFullName);
if (fileFullName.lastIndexOf(".") != -1 && fileFullName.lastIndexOf(".") != 0) {
fileType = fileFullName.substring(fileFullName.lastIndexOf(".") + 1);
fileName = fileFullName.substring(fileFullName.lastIndexOf("."));
}
// 判断文件名称是否存在 // 判断文件名称是否存在
EDDataParams folderParames = new EDDataParams(); EDDataParams folderParames = new EDDataParams();
@ -343,7 +343,7 @@ public class EDDataServiceImpl implements EDDataService {
edDataInfo.setModifier(parames.getUserId()); edDataInfo.setModifier(parames.getUserId());
edDataInfo.setModifierName(parames.getUserName()); edDataInfo.setModifierName(parames.getUserName());
edDataInfo.setCategoryId(parames.getParentId()); edDataInfo.setCategoryId(parames.getParentId());
edDataInfo.setDataName(fileFullName); edDataInfo.setDataName(fileName);
edDataInfo.setNote(parames.getNote()); edDataInfo.setNote(parames.getNote());
edDataInfo.setFileType(fileType); edDataInfo.setFileType(fileType);
edDataInfo.setGmtBatchUpload(parames.getGmtBatchUpload()); edDataInfo.setGmtBatchUpload(parames.getGmtBatchUpload());

View File

@ -0,0 +1,25 @@
package electromagnetic.data.framework.share.util;
public final class EleCommonUtil {
public static String getFileName(String fileFullName) {
if (fileFullName == null) {
return "";
}
if (!fileFullName.contains(".")) {
return fileFullName;
}
int index = fileFullName.lastIndexOf(".");
return fileFullName.substring(0, index);
}
public static String getFileType(String fileFullName) {
if (fileFullName == null || !fileFullName.contains(".")) {
return "";
}
int index = fileFullName.lastIndexOf(".");
return fileFullName.substring(index + 1);
}
}

View File

@ -4,6 +4,7 @@ import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil; import cn.hutool.core.util.ZipUtil;
import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES; import cn.hutool.crypto.symmetric.AES;
@ -194,7 +195,8 @@ public class EDDataFacadeImpl implements EDDataFacade {
EDDataInfo edDataInfo = edDataService.getDataInfo(parames); EDDataInfo edDataInfo = edDataService.getDataInfo(parames);
Assert.isTrue(edDataInfo != null, "没有找到该下载文件"); Assert.isTrue(edDataInfo != null, "没有找到该下载文件");
String filePathOfFolder = edDataService.getFilePathOfFolder(edDataInfo.getCategoryId()); String filePathOfFolder = edDataService.getFilePathOfFolder(edDataInfo.getCategoryId());
String filePath = storageFilePath + "/" + filePathOfFolder + "/" + edDataInfo.getDataName(); String fileType = StrUtil.isEmpty(edDataInfo.getFileType()) ? "" : "." + edDataInfo.getFileType();
String filePath = storageFilePath + "/" + filePathOfFolder + "/" + edDataInfo.getDataName() + fileType;
Assert.isTrue(FileUtil.exist(filePath), "下载文件不存在。"); Assert.isTrue(FileUtil.exist(filePath), "下载文件不存在。");
FileSystemResource fileSystemResource = new FileSystemResource(filePath); FileSystemResource fileSystemResource = new FileSystemResource(filePath);