Эх сурвалжийг харах

试验-规范管理-规范文件批量更新 异常后删除上传成功的文件

LHB 2 сар өмнө
parent
commit
8e961368fd

+ 16 - 4
blade-service/blade-business/src/main/java/org/springblade/business/service/impl/PrivateStandardServiceImpl.java

@@ -218,6 +218,8 @@ public class PrivateStandardServiceImpl extends ServiceImpl<PrivateStandardMappe
             e.printStackTrace();
             throw new ServiceException("Oss删除文件失败");
         }
+        //文件集合
+        List<StandardFile> standardFiles = new ArrayList<>();
         try {
 
             // 文件索引计数器
@@ -243,15 +245,16 @@ public class PrivateStandardServiceImpl extends ServiceImpl<PrivateStandardMappe
 
                         //Oss上传 传特殊文件名 在oss中做特殊路径处理
                         BladeFile bladeFile = newIOSSClient.uploadFileByInputStream(multipartFile);
+                        if (bladeFile == null) {
+                            throw new ServiceException("Oss异常");
+                        }
                         standardFile.setStandardFileUrl(bladeFile.getLink());
-
                         //添加新文件
                         standardFileService.save(standardFile);
                     }
                 }
 
 
-
                 //创建数据
                 PrivateStandard privateStandard = BeanUtil.copyProperties(dto, PrivateStandard.class);
                 //修改
@@ -259,8 +262,17 @@ public class PrivateStandardServiceImpl extends ServiceImpl<PrivateStandardMappe
                 return true;
             }
             return false;
-        } catch (IOException e) {
-            throw new RuntimeException(e);
+        } catch (Exception e) {
+            //删除之前上传的文件
+            if(CollectionUtils.isNotEmpty(standardFiles)){
+                for (StandardFile standardFile : standardFiles) {
+                    String pdfName = standardFile.getStandardFileUrl().split("upload")[1];
+                    this.newIOSSClient.removeFile("upload" + pdfName);
+                }
+                throw new ServiceException("Oss异常");
+            }
+            e.printStackTrace();
+            throw new ServiceException("更新失败");
         }
     }
 }