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

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<>(); List<String> pathList = new ArrayList<>();
for(AccountHead accountHead: list){ for(AccountHead accountHead: list){
sb.append("[").append(accountHead.getBillNo()).append("]"); sb.append("[").append(accountHead.getBillNo()).append("]");
pathList.add(accountHead.getFileName()); if(StringUtil.isNotEmpty(accountHead.getFileName())) {
pathList.add(accountHead.getFileName());
}
if("1".equals(accountHead.getStatus())) { if("1".equals(accountHead.getStatus())) {
throw new BusinessRunTimeException(ExceptionConstants.ACCOUNT_HEAD_UN_AUDIT_DELETE_FAILED_CODE, throw new BusinessRunTimeException(ExceptionConstants.ACCOUNT_HEAD_UN_AUDIT_DELETE_FAILED_CODE,
String.format(ExceptionConstants.ACCOUNT_HEAD_UN_AUDIT_DELETE_FAILED_MSG)); String.format(ExceptionConstants.ACCOUNT_HEAD_UN_AUDIT_DELETE_FAILED_MSG));

View File

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

View File

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

View File

@@ -364,32 +364,33 @@ public class SystemConfigService {
if(fileUploadType == 1) { if(fileUploadType == 1) {
//本地 //本地
for(String pathStr: pathList) { for(String pathStr: pathList) {
String[] pathArr = pathStr.split(","); if(StringUtil.isNotEmpty(pathStr)) {
for (int i = 0; i < pathArr.length; i++) { String[] pathArr = pathStr.split(",");
String path = pathArr[i]; for (String path : pathArr) {
// 提取文件的路径 // 提取文件的路径
String pathDir = getDirByPath(path); String pathDir = getDirByPath(path);
if(StringUtil.isNotEmpty(pathDir)) { if (StringUtil.isNotEmpty(pathDir)) {
// 源文件路径 // 源文件路径
Path sourcePath = Paths.get(filePath + File.separator + path); Path sourcePath = Paths.get(filePath + File.separator + path);
// 目标文件路径(注意这里是新文件的完整路径,包括文件名) // 目标文件路径(注意这里是新文件的完整路径,包括文件名)
Path targetPath = Paths.get(filePath + File.separator + DELETED + File.separator + path); Path targetPath = Paths.get(filePath + File.separator + DELETED + File.separator + path);
try { try {
File file = new File(filePath + File.separator + DELETED + File.separator + pathDir); File file = new File(filePath + File.separator + DELETED + File.separator + pathDir);
if (!file.exists()) { if (!file.exists()) {
file.mkdirs();// 创建文件根目录 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 accessKeySecret = platformConfigService.getPlatformConfigByKey("aliOss_accessKeySecret").getPlatformValue();
String bucketName = platformConfigService.getPlatformConfigByKey("aliOss_bucketName").getPlatformValue(); String bucketName = platformConfigService.getPlatformConfigByKey("aliOss_bucketName").getPlatformValue();
for(String pathStr: pathList) { for(String pathStr: pathList) {
String[] pathArr = pathStr.split(","); if(StringUtil.isNotEmpty(pathStr)) {
for (int i = 0; i < pathArr.length; i++) { String[] pathArr = pathStr.split(",");
String path = pathArr[i]; for (String path : pathArr) {
// 创建OSSClient实例。 if(StringUtil.isNotEmpty(path)) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 创建OSSClient实例。
try { OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String filePathStr = StringUtil.isNotEmpty(filePath)? filePath.substring(1):""; try {
String sourceObjectKey = filePathStr + "/" + path; String filePathStr = StringUtil.isNotEmpty(filePath) ? filePath.substring(1) : "";
String sourceSmallObjectKey = filePathStr + "-small/" + path; String sourceObjectKey = filePathStr + "/" + path;
String destinationObjectKey = DELETED + "/list/" + sourceObjectKey; String sourceSmallObjectKey = filePathStr + "-small/" + path;
String destinationSmallObjectKey = DELETED + "/list/" + sourceSmallObjectKey; String destinationObjectKey = DELETED + "/list/" + sourceObjectKey;
this.copySourceToDest(ossClient, bucketName, sourceObjectKey, destinationObjectKey); String destinationSmallObjectKey = DELETED + "/list/" + sourceSmallObjectKey;
this.copySourceToDest(ossClient, bucketName, sourceSmallObjectKey, destinationSmallObjectKey); this.copySourceToDest(ossClient, bucketName, sourceObjectKey, destinationObjectKey);
} catch (Exception e) { this.copySourceToDest(ossClient, bucketName, sourceSmallObjectKey, destinationSmallObjectKey);
logger.error(e.getMessage()); } catch (Exception e) {
} finally { logger.error(e.getMessage());
// 关闭OSSClient。 } finally {
if (ossClient != null) { // 关闭OSSClient。
ossClient.shutdown(); if (ossClient != null) {
ossClient.shutdown();
}
}
} }
} }
} }