解决删除图片遇到的空指针异常,增加判断

This commit is contained in:
jishenghua 2024-12-05 21:27:53 +08:00
parent 4ab7884641
commit e49b8e05d4
4 changed files with 57 additions and 47 deletions

View File

@ -228,7 +228,9 @@ public class AccountHeadService {
List<String> pathList = new ArrayList<>();
for(AccountHead accountHead: list){
sb.append("[").append(accountHead.getBillNo()).append("]");
pathList.add(accountHead.getFileName());
if(StringUtil.isNotEmpty(accountHead.getFileName())) {
pathList.add(accountHead.getFileName());
}
if("1".equals(accountHead.getStatus())) {
throw new BusinessRunTimeException(ExceptionConstants.ACCOUNT_HEAD_UN_AUDIT_DELETE_FAILED_CODE,
String.format(ExceptionConstants.ACCOUNT_HEAD_UN_AUDIT_DELETE_FAILED_MSG));

View File

@ -450,7 +450,9 @@ public class DepotHeadService {
List<DepotHead> dhList = getDepotHeadListByIds(ids);
for(DepotHead depotHead: dhList){
sb.append("[").append(depotHead.getNumber()).append("]");
pathList.add(depotHead.getFileName());
if(StringUtil.isNotEmpty(depotHead.getFileName())) {
pathList.add(depotHead.getFileName());
}
//只有未审核的单据才能被删除
if("0".equals(depotHead.getStatus())) {
User userInfo = userService.getCurrentUser();

View File

@ -299,7 +299,9 @@ public class MaterialService {
List<Material> list = getMaterialListByIds(ids);
for(Material material: list){
sb.append("[").append(material.getName()).append("]");
pathList.add(material.getImgName());
if(StringUtil.isNotEmpty(material.getImgName())) {
pathList.add(material.getImgName());
}
}
logService.insertLog("商品", sb.toString(),
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());

View File

@ -364,32 +364,33 @@ public class SystemConfigService {
if(fileUploadType == 1) {
//本地
for(String pathStr: pathList) {
String[] pathArr = pathStr.split(",");
for (int i = 0; i < pathArr.length; i++) {
String path = pathArr[i];
// 提取文件的路径
String pathDir = getDirByPath(path);
if(StringUtil.isNotEmpty(pathDir)) {
// 源文件路径
Path sourcePath = Paths.get(filePath + File.separator + path);
// 目标文件路径注意这里是新文件的完整路径包括文件名
Path targetPath = Paths.get(filePath + File.separator + DELETED + File.separator + path);
try {
File file = new File(filePath + File.separator + DELETED + File.separator + pathDir);
if (!file.exists()) {
file.mkdirs();// 创建文件根目录
if(StringUtil.isNotEmpty(pathStr)) {
String[] pathArr = pathStr.split(",");
for (String path : pathArr) {
// 提取文件的路径
String pathDir = getDirByPath(path);
if (StringUtil.isNotEmpty(pathDir)) {
// 源文件路径
Path sourcePath = Paths.get(filePath + File.separator + path);
// 目标文件路径注意这里是新文件的完整路径包括文件名
Path targetPath = Paths.get(filePath + File.separator + DELETED + File.separator + path);
try {
File file = new File(filePath + File.separator + DELETED + File.separator + pathDir);
if (!file.exists()) {
file.mkdirs();// 创建文件根目录
}
// 复制文件如果目标文件已存在则替换它
Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
// 删除源文件
Files.delete(sourcePath);
logger.info("File copied successfully.");
} catch (NoSuchFileException e) {
logger.error("Source file not found: " + e.getMessage());
} catch (IOException e) {
logger.error("An I/O error occurred: " + e.getMessage());
} catch (SecurityException e) {
logger.error("No permission to copy file: " + e.getMessage());
}
// 复制文件如果目标文件已存在则替换它
Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
// 删除源文件
Files.delete(sourcePath);
logger.info("File copied successfully.");
} catch (NoSuchFileException e) {
logger.error("Source file not found: " + e.getMessage());
} catch (IOException e) {
logger.error("An I/O error occurred: " + e.getMessage());
} catch (SecurityException e) {
logger.error("No permission to copy file: " + e.getMessage());
}
}
}
@ -401,25 +402,28 @@ public class SystemConfigService {
String accessKeySecret = platformConfigService.getPlatformConfigByKey("aliOss_accessKeySecret").getPlatformValue();
String bucketName = platformConfigService.getPlatformConfigByKey("aliOss_bucketName").getPlatformValue();
for(String pathStr: pathList) {
String[] pathArr = pathStr.split(",");
for (int i = 0; i < pathArr.length; i++) {
String path = pathArr[i];
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
String filePathStr = StringUtil.isNotEmpty(filePath)? filePath.substring(1):"";
String sourceObjectKey = filePathStr + "/" + path;
String sourceSmallObjectKey = filePathStr + "-small/" + path;
String destinationObjectKey = DELETED + "/list/" + sourceObjectKey;
String destinationSmallObjectKey = DELETED + "/list/" + sourceSmallObjectKey;
this.copySourceToDest(ossClient, bucketName, sourceObjectKey, destinationObjectKey);
this.copySourceToDest(ossClient, bucketName, sourceSmallObjectKey, destinationSmallObjectKey);
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
// 关闭OSSClient
if (ossClient != null) {
ossClient.shutdown();
if(StringUtil.isNotEmpty(pathStr)) {
String[] pathArr = pathStr.split(",");
for (String path : pathArr) {
if(StringUtil.isNotEmpty(path)) {
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
String filePathStr = StringUtil.isNotEmpty(filePath) ? filePath.substring(1) : "";
String sourceObjectKey = filePathStr + "/" + path;
String sourceSmallObjectKey = filePathStr + "-small/" + path;
String destinationObjectKey = DELETED + "/list/" + sourceObjectKey;
String destinationSmallObjectKey = DELETED + "/list/" + sourceSmallObjectKey;
this.copySourceToDest(ossClient, bucketName, sourceObjectKey, destinationObjectKey);
this.copySourceToDest(ossClient, bucketName, sourceSmallObjectKey, destinationSmallObjectKey);
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
// 关闭OSSClient
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
}