Ver Fonte

[Fix-7154][API] Fix MultipartFile resource [file] cannot be resolved to absolute file… (#7155)

* fix MultipartFile resource [file] cannot be resolved to absolute file path

* update testCopyFile.
Kerwin há 3 anos atrás
pai
commit
8f77fbbeb8

+ 1 - 1
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java

@@ -596,7 +596,7 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe
             if (!HadoopUtils.getInstance().exists(resourcePath)) {
                 createTenantDirIfNotExists(tenantCode);
             }
-            org.apache.dolphinscheduler.api.utils.FileUtils.copyFile(file, localFilename);
+            org.apache.dolphinscheduler.api.utils.FileUtils.copyInputStreamToFile(file, localFilename);
             HadoopUtils.getInstance().copyLocalToHdfs(localFilename, hdfsFilename, true, true);
         } catch (Exception e) {
             FileUtils.deleteFile(localFilename);

+ 5 - 6
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/FileUtils.java

@@ -38,14 +38,13 @@ public class FileUtils {
     private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
 
     /**
-     * copy source file to target file
-     *
-     * @param file         file
-     * @param destFilename destination file name
+     * copy source InputStream to target file
+     * @param file
+     * @param destFilename
      */
-    public static void copyFile(MultipartFile file, String destFilename) {
+    public static void copyInputStreamToFile(MultipartFile file, String destFilename) {
         try {
-            org.apache.commons.io.FileUtils.copyFile(file.getResource().getFile(), new File(destFilename));
+            org.apache.commons.io.FileUtils.copyInputStreamToFile(file.getInputStream(), new File(destFilename));
         } catch (IOException e) {
             logger.error("failed to copy file , {} is empty file", file.getOriginalFilename(), e);
         }

+ 2 - 2
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/utils/FileUtilsTest.java

@@ -73,10 +73,10 @@ public class FileUtilsTest {
 
         //Use Mockito to mock MultipartFile
         MultipartFile file = Mockito.mock(MultipartFile.class, Mockito.RETURNS_DEEP_STUBS);
-        Mockito.when(file.getResource().getFile()).thenReturn(new File(src));
+        Mockito.when(file.getInputStream()).thenReturn(new FileInputStream(src));
 
         //Invoke copyFile
-        FileUtils.copyFile(file,destFilename);
+        FileUtils.copyInputStreamToFile(file,destFilename);
 
         //Test file exists
         File destFile = new File(destFilename);