Browse Source

[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.
3.0.0/version-upgrade
Kerwin 3 years ago committed by GitHub
parent
commit
8f77fbbeb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java
  2. 11
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/FileUtils.java
  3. 4
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/utils/FileUtilsTest.java

2
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);

11
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);
}

4
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);

Loading…
Cancel
Save