优化删除图片逻辑,对文件不存在的情况处理

Signed-off-by: zup <jiu3295282258@163.com>
main
laptop_zup 9 months ago committed by zup
parent 3c9cd07a5a
commit e957e964e3

@ -131,22 +131,27 @@ public class AppFileUtils {
*
* @param oldPath
*/
public static void removeFileByPath(String oldPath) {
// 图片的路径不是默认图片的路径
if (!oldPath.endsWith(Constast.DEFAULT_IMG_GOODS) && !oldPath.endsWith(Constast.DEFAULT_IMG_USER)) {
public static boolean removeFileByPath(String oldPath) {
// 图片的路径是默认用户头像则不执行删除操作
if (oldPath != null && oldPath.endsWith(Constast.DEFAULT_IMG_USER)) {
return true;
} else {
assert oldPath != null;
File file = new File(UPLOAD_PATH, oldPath);
// 文件存在则删除
if (file.exists()) {
if (!file.delete()) {
// 文件删除失败,抛出异常
if (file.delete()) {
return true;
} else {
throw new RuntimeException("文件删除失败: " + file.getAbsolutePath());
}
} else {
// 文件不存在,抛出异常
throw new RuntimeException("文件不存在: " + file.getAbsolutePath());
// 文件不存在
return false;
}
}
}
}
}
Loading…
Cancel
Save