Forráskód Böngészése

内网FTP主动获取修改为被动

wukai 2 éve
szülő
commit
ebcdbaa4ab

+ 17 - 0
sync-admin/src/test/java/FtpTest.java

@@ -0,0 +1,17 @@
+import cn.hutool.extra.ftp.Ftp;
+import cn.hutool.extra.ftp.FtpMode;
+
+import java.io.File;
+
+public class FtpTest {
+    public static void main(String[] args) {
+        File file = new File("D:\\SYSTEM\\Desktop\\temp\\logo.png");
+        try (Ftp ftp = new Ftp("localhost", 10020, "ftpadmin", "ftpadmin")) {
+            // 设置传输模式为被动模式(可根据需要修改)
+            ftp.setMode(FtpMode.Passive);
+            ftp.upload("/upload", file);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 35 - 0
sync-in/src/main/java/com/jjt/in/service/impl/InProcessServiceImpl.java

@@ -64,10 +64,45 @@ public class InProcessServiceImpl extends InBaseService implements IInProcessSer
     }
 
     /**
+     * 从FTP接收目录拷入到正式同步目录
+     */
+    private void cpFile() {
+        String ftpDir = sysConfigService.selectConfigByKey("in.ftp.dir");
+        //获取内网同步正式目录
+        String syncDir = syncDIr();
+        // 创建File对象
+        File dirFile = new File(ftpDir);
+        if (dirFile.isDirectory()) {
+            //先获取目录下所有json格式文件
+            File[] files = dirFile.listFiles(((dir, name) -> name.endsWith(".json")));
+            if (files != null && files.length > 0) {
+                Arrays.sort(files, Comparator.comparing(File::getName));
+            }
+
+            for (File file : files) {
+                try {
+                    ObjectMapper mapper = new ObjectMapper();
+                    FileDesc desc = mapper.readValue(file, FileDesc.class);
+                    File zipFile = new File(ftpDir + desc.getName());
+                    String md5 = DigestUtils.md5Hex(Files.newInputStream(zipFile.toPath()));
+                    if (md5.equals(desc.getMd5())) {
+                        Files.move(file.toPath(), Paths.get(syncDir + file.getName()), StandardCopyOption.REPLACE_EXISTING);
+                        Files.move(zipFile.toPath(), Paths.get(syncDir + zipFile.getName()), StandardCopyOption.REPLACE_EXISTING);
+                    }
+                } catch (Exception e) {
+
+                }
+            }
+
+        }
+    }
+
+    /**
      * 解压文件
      */
     @Override
     public void unzip() {
+        cpFile();
         //获取内网同步正式目录
         String syncDir = syncDIr();
         //获取内网临时目录

+ 15 - 14
sync-in/src/main/java/com/jjt/in/task/InProcessTask.java

@@ -18,20 +18,21 @@ public class InProcessTask {
 
     public void sync() {
 
-        try {
-            processService.downloadFtpFile();
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        System.out.printf("下载完成");
-
-        try {
-            processService.deleteFtpFile();
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        System.out.printf("删除完成");
+//        try {
+//            processService.downloadFtpFile();
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//
+//        System.out.printf("下载完成");
+//
+//        try {
+//            processService.deleteFtpFile();
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//        System.out.printf("删除完成");
+
 
 
         System.out.printf("开始处理");