Jelajahi Sumber

修改文件上传下载逻辑

tianboguang 2 tahun lalu
induk
melakukan
a1e278bd08

+ 45 - 2
4dkankan-utils-fyun-https/src/main/java/com/fdkankan/fyun/http/HttpFileService.java

@@ -94,6 +94,9 @@ public class HttpFileService extends AbstractFYunFileService {
 
             if (filePath.startsWith(nasBasePath)) {
                 filePath = filePath.replace(nasBasePath, "");
+            }else{
+                log.info("先将文件{}拷贝至{}", filePath, nasBasePath + filePath);
+                FileUtils.copyFile(filePath, nasBasePath + filePath, true);
             }
 
             if (!remoteFilePath.startsWith(File.separator)) {
@@ -129,6 +132,11 @@ public class HttpFileService extends AbstractFYunFileService {
 
         log.info("uploadFileByCommand,bucket:{},filePath:{},remoteFilePath:{}", bucket, filePath, remoteFilePath);
 
+        if (filePath.substring(filePath.lastIndexOf(File.separator)).contains(".")) {
+            log.info("转上传文件");
+            return uploadFile(bucket, filePath, remoteFilePath);
+        }
+
         // 上传文件夹
         Map<String, Object> params = new HashMap<>();
         params.put("appName", fYunFileConfig.getKey());
@@ -136,6 +144,9 @@ public class HttpFileService extends AbstractFYunFileService {
 
         if (filePath.startsWith(nasBasePath)) {
             filePath = filePath.replace(nasBasePath, "");
+        }else{
+            log.info("先将文件{}拷贝至{}", filePath, nasBasePath + filePath);
+            FileUtils.copyFile(filePath, nasBasePath + filePath, true);
         }
 
         if (filePath.endsWith(File.separator)) {
@@ -167,6 +178,10 @@ public class HttpFileService extends AbstractFYunFileService {
     @Override
     public void downloadFileByCommand(String bucket, String localPath, String remoteFilePath) {
         log.info("Fyun http utils download folder by command ,bucket:{},localPath:{},remoteFilePath:{}", bucket, localPath, remoteFilePath);
+        if(remoteFilePath.substring(remoteFilePath.lastIndexOf(File.separator)).contains(".")){
+            log.info("转下载文件");
+            downloadFile(remoteFilePath,localPath);
+        }
         // 下载文件夹
         Map<String, Object> params = new HashMap<>();
         params.put("appName", fYunFileConfig.getKey());
@@ -198,6 +213,8 @@ public class HttpFileService extends AbstractFYunFileService {
         log.info("Fyun Http Utils download folder,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
         if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
             log.error("Fyun Http Utils download folder failed!");
+        }else{
+            waitFileReadable(nasBasePath+localPath);
         }
     }
 
@@ -305,7 +322,7 @@ public class HttpFileService extends AbstractFYunFileService {
                 sourcePath = File.separator.concat(sourcePath);
             }
 
-            if (new File(sourcePath).isDirectory()) {
+            if (!sourcePath.substring(sourcePath.lastIndexOf(File.separator)).contains(".")) {
                 if (sourcePath.endsWith(File.separator)) {
                     sourcePath = sourcePath.substring(0, sourcePath.length() - 1);
                 }
@@ -414,11 +431,15 @@ public class HttpFileService extends AbstractFYunFileService {
             params.put("secret", fYunFileConfig.getSecret());
 
             if (!remoteFilePath.startsWith(File.separator)) {
-                localPath = File.separator.concat(remoteFilePath);
+                remoteFilePath = File.separator.concat(remoteFilePath);
             }
 
             params.put("fileName", remoteFilePath);
 
+            if (new File(localPath).exists()) {
+                FileUtils.deleteFile(localPath);
+            }
+
             if (localPath.startsWith(nasBasePath)) {
                 localPath = localPath.replace(nasBasePath, "");
             }
@@ -432,6 +453,8 @@ public class HttpFileService extends AbstractFYunFileService {
             log.info("Fyun Http Utils download file,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
             if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
                 log.error("Fyun Http Utils download file failed!");
+            }else{
+                waitFileReadable(nasBasePath+localPath);
             }
         } catch (Throwable throwable) {
             log.error("文件下载失败:{}", remoteFilePath);
@@ -449,4 +472,24 @@ public class HttpFileService extends AbstractFYunFileService {
         return 0;
     }
 
+    private void waitFileReadable(String path){
+        if (new File(path).exists()) {
+            log.info("文件已存在:{}", path);
+            return;
+        }
+        for (long i = 0; i < 10000000000L; i++) {
+            log.error("开始第{}次检查文件:{}", i + 1, path);
+            try {
+                Thread.sleep(5000);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            if (new File(path).exists()) {
+                return;
+            }
+        }
+        log.error("文件不存在:{}", path);
+
+    }
+
 }