Parcourir la source

修复多文件上传文件至FTP 重复连接问题

wukai il y a 1 an
Parent
commit
e9a07015d4

+ 4 - 5
sync-common/src/main/java/com/jjt/common/utils/FtpUtil.java

@@ -80,7 +80,7 @@ public class FtpUtil {
             }
             return true;
         } catch (Exception e) {
-            log.error("下载文件失败啦:", e.getMessage());
+            log.error("下载文件失败啦:{}", e.getMessage());
             return false;
         }
     }
@@ -112,7 +112,7 @@ public class FtpUtil {
                 }
 
             } catch (Exception e) {
-                log.error("上传出错啦", e.getMessage());
+                log.error("上传出错啦{}", e.getMessage());
                 return false;
             }
         }
@@ -132,10 +132,9 @@ public class FtpUtil {
         try (Ftp ftp = new Ftp(ip, port == null ? 21 : port, user, pass)) {
             // 设置传输模式为被动模式(可根据需要修改)
             ftp.setMode(FtpMode.Passive);
-
             return ftp.upload(dir, file);
         } catch (Exception e) {
-            log.error("上传出错啦", e.getMessage());
+            log.error("上传出错啦{}", e.getMessage());
             return false;
         }
     }
@@ -162,7 +161,7 @@ public class FtpUtil {
             }
 
         } catch (Exception e) {
-            log.error("删除出错啦", e.getMessage());
+            log.error("删除出错啦{}", e.getMessage());
             return false;
         }
         return true;

+ 18 - 10
sync-out/src/main/java/com/jjt/out/service/impl/OutProcessServiceImpl.java

@@ -1,5 +1,7 @@
 package com.jjt.out.service.impl;
 
+import cn.hutool.extra.ftp.Ftp;
+import cn.hutool.extra.ftp.FtpMode;
 import com.alibaba.fastjson.JSONObject;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.jjt.common.constant.Constants;
@@ -58,10 +60,16 @@ public class OutProcessServiceImpl extends OutBaseService implements IOutProcess
                 if (files != null && files.length > 0) {
                     Arrays.sort(files, Comparator.comparing(File::getName));
                 }
+                if (!ftpUtil.getDir().endsWith(Constants.DIR_END)) {
+                    ftpUtil.setDir(ftpUtil.getDir() + "/");
+                }
+                try (Ftp ftp = new Ftp(ftpUtil.getIp(), ftpUtil.getPort() == null ? 21 : ftpUtil.getPort(), ftpUtil.getUser(), ftpUtil.getPass())) {
+
+                    // 设置传输模式为被动模式(可根据需要修改)
+                    ftp.setMode(FtpMode.Passive);
 
-                for (File file : files) {
-                    //获取文件同步顺序,如果报错,则不同步
-                    try {
+                    for (File file : files) {
+                        //获取文件同步顺序,如果报错,则不同步
                         //校验JSON文件名格式
                         if (!file.getName().startsWith("sync-")) {
                             continue;
@@ -75,7 +83,7 @@ public class OutProcessServiceImpl extends OutBaseService implements IOutProcess
                         if (md5.equals(desc.getMd5())) {
                             Date start = new Date();
                             //上传json文件
-                            boolean flag = ftpUtil.upload(file);
+                            boolean flag = ftp.upload(ftpUtil.getDir(), file);
                             boolean flag1 = false;
                             //上传zip文件
                             List<File> list = new ArrayList<>();
@@ -85,13 +93,13 @@ public class OutProcessServiceImpl extends OutBaseService implements IOutProcess
                                     //如果是分卷压缩文件,则需要读取所有文件名上传
                                     list = zipFile.getSplitZipFiles();
                                     for (File f : list) {
-                                        flag1 = flag && ftpUtil.upload(f);
+                                        flag1 = flag && ftp.upload(ftpUtil.getDir(), f);
                                     }
                                 } else {
-                                    flag1 = ftpUtil.upload(zipFile.getFile());
+                                    flag1 = ftp.upload(ftpUtil.getDir(), zipFile.getFile());
                                 }
                             } else {
-                                flag1 = ftpUtil.upload(syncFile);
+                                flag1 = ftp.upload(ftpUtil.getDir(), syncFile);
                             }
 
                             Date end = new Date();
@@ -118,11 +126,11 @@ public class OutProcessServiceImpl extends OutBaseService implements IOutProcess
                             info.setUploadStatus(status);
                             syncInfoService.insertOutSyncInfo(info);
                         } else {
-                            log.error("这个是错误滴" + desc.toString());
+                            log.error("MD5校验未成功,暂时不上传" + desc.toString());
                         }
-                    } catch (Exception e) {
-                        e.printStackTrace();
                     }
+                } catch (Exception e) {
+                    log.error("上传出错啦{}", e.getMessage());
                 }
             }
         } catch (Exception e) {