Browse Source

Merge remote-tracking branch 'origin/write-file-error' into projects/gouli

lyhzzz 1 year ago
parent
commit
8353408df2

+ 6 - 4
src/main/java/com/fdkankan/ucenter/service/impl/SceneCommonService.java

@@ -32,6 +32,8 @@ public class SceneCommonService {
     FYunFileServiceInterface fYunFileServiceInterface;
     @Autowired
     IScene3dNumService scene3dNumService;
+    @Autowired
+    com.fdkankan.ucenter.util.FileUtil fileUtil;
 
     public String getNewNum(String oldNum ){
         String newNum = scene3dNumService.generateSceneNum(1);
@@ -141,11 +143,11 @@ public class SceneCommonService {
                     if(fileName.contains("scene.json")){
                         JSONObject jsonObject = JSONObject.parseObject(newJson);
                         jsonObject.put("sceneName",newSceneName);
-                        com.fdkankan.ucenter.util.FileUtil.writeFile(localPath,jsonObject.toJSONString());
+                        fileUtil.writeFile(localPath,jsonObject.toJSONString());
                         String sceneJsonPath = String.format(SceneResourcePath.dataPath+"/"+fileName, newNum);
                         fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
                     }else {
-                        com.fdkankan.ucenter.util.FileUtil.writeFile(localPath,newJson);
+                        fileUtil.writeFile(localPath,newJson);
                     }
 
 
@@ -153,7 +155,7 @@ public class SceneCommonService {
                 if("v4".equals(sceneVersion)){
                     JSONObject jsonObject = JSONObject.parseObject(newJson);
                     jsonObject.put("title",newSceneName);
-                    com.fdkankan.ucenter.util.FileUtil.writeFile(localPath,jsonObject.toJSONString());
+                    fileUtil.writeFile(localPath,jsonObject.toJSONString());
 
                     String sceneJsonPath = String.format(SceneResourcePath.DATA_VIEW_PATH+"/" + fileName, newNum);
                     fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
@@ -194,7 +196,7 @@ public class SceneCommonService {
         if(StringUtils.isNotBlank(fileContent)){
             String newJson = fileContent.replaceAll(oldNum,newNum);
             try {
-                com.fdkankan.ucenter.util.FileUtil.writeFile(localPath,newJson);
+                fileUtil.writeFile(localPath,newJson);
                 fYunFileServiceInterface.uploadFile(localPath,ossStatusJsonPath);
             }catch (Exception e){
                 log.error("writeFile-error:{}",e);

+ 31 - 12
src/main/java/com/fdkankan/ucenter/util/FileUtil.java

@@ -1,21 +1,40 @@
 package com.fdkankan.ucenter.util;
 
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
 import java.io.*;
 
+@Slf4j
+@Component
 public class FileUtil {
 
-    public static void writeFile(String filePath,String str) throws IOException {
-
-        File fout = new File(filePath);
-        if(!fout.getParentFile().exists()){
-            fout.getParentFile().mkdirs();
+    public  void writeFile(String filePath,String str) {
+        FileOutputStream fos = null;
+        BufferedWriter bw = null;
+        try {
+            File fout = new File(filePath);
+            if(!fout.getParentFile().exists()){
+                fout.getParentFile().mkdirs();
+            }
+            if(!fout.exists()){
+                fout.createNewFile();
+            }
+            fos = new FileOutputStream(fout);
+            bw = new BufferedWriter(new OutputStreamWriter(fos));
+            bw.write(str);
+        }catch (Exception e){
+            log.info("writeFile:error:{}",e);
+        }finally {
+            if(bw != null){
+                try {
+                    bw.close();
+                    fos.close();
+                }catch (Exception e){
+                    log.info("writeFile:close-error:{}",e);
+                }
+            }
         }
-        if(!fout.exists()){
-            fout.createNewFile();
-        }
-        FileOutputStream fos = new FileOutputStream(fout);
-        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
-        bw.write(str);
-        bw.close();
+
     }
 }