Pārlūkot izejas kodu

Close the FileOutputStream in FileUtils.java file (#7937)

Close the FileOutputStream in FileUtils.java file
Ivan0626 3 gadi atpakaļ
vecāks
revīzija
b807c677ab

+ 5 - 1
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java

@@ -138,16 +138,20 @@ public class FileUtils {
      * @return true if write success
      * @return true if write success
      */
      */
     public static boolean writeContent2File(String content, String filePath) {
     public static boolean writeContent2File(String content, String filePath) {
+        FileOutputStream fos = null;
         try {
         try {
             File distFile = new File(filePath);
             File distFile = new File(filePath);
             if (!distFile.getParentFile().exists() && !distFile.getParentFile().mkdirs()) {
             if (!distFile.getParentFile().exists() && !distFile.getParentFile().mkdirs()) {
                 logger.error("mkdir parent failed");
                 logger.error("mkdir parent failed");
                 return false;
                 return false;
             }
             }
-            IOUtils.write(content, new FileOutputStream(filePath), StandardCharsets.UTF_8);
+            fos = new FileOutputStream(filePath);
+            IOUtils.write(content, fos, StandardCharsets.UTF_8);
         } catch (IOException e) {
         } catch (IOException e) {
             logger.error(e.getMessage(), e);
             logger.error(e.getMessage(), e);
             return false;
             return false;
+        } finally {
+            IOUtils.closeQuietly(fos);
         }
         }
         return true;
         return true;
     }
     }