Procházet zdrojové kódy

调整上传FTP为 复制到本地同步目录。

wukai před 1 rokem
rodič
revize
06a34853fa

+ 6 - 0
sync-out/src/main/java/com/jjt/out/service/IOutProcessService.java

@@ -13,6 +13,11 @@ public interface IOutProcessService {
     public void sync();
 
     /**
+     * 复制文件
+     */
+    public void copy();
+
+    /**
      * 上传文件到光闸FTP
      */
     public void uploadFile();
@@ -24,6 +29,7 @@ public interface IOutProcessService {
 
     /**
      * 删除过期备份文件
+     *
      * @param day 指定过期天数
      */
     void clean(int day);

+ 96 - 0
sync-out/src/main/java/com/jjt/out/service/impl/OutProcessServiceImpl.java

@@ -22,6 +22,7 @@ import javax.annotation.Resource;
 import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.StandardCopyOption;
 import java.util.*;
@@ -141,6 +142,100 @@ public class OutProcessServiceImpl extends OutBaseService implements IOutProcess
     }
 
     /**
+     * 同步文件
+     */
+    @Override
+    public void copy() {
+        //获取外网同步正式目录
+        String syncDir = syncDIr();
+        //获取外网同步备份目录
+        String bakDir = bakDir();
+        String target="/home/ftp/upload/";
+
+        try {
+            // 创建File对象
+            File dirFile = new File(syncDir);
+            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) {
+                    //获取文件同步顺序,如果报错,则不同步
+                    //校验JSON文件名格式
+                    if (!file.getName().startsWith("sync-")) {
+                        continue;
+                    }
+                    long sort = Long.parseLong(file.getName().split("-")[1]);
+                    ObjectMapper mapper = new ObjectMapper();
+                    FileDesc desc = mapper.readValue(file, FileDesc.class);
+                    boolean isZip = desc.getName().indexOf(".zip") != -1;
+                    File syncFile = new File(syncDir + desc.getName());
+                    String md5 = DigestUtils.md5Hex(Files.newInputStream(syncFile.toPath()));
+                    if (md5.equals(desc.getMd5())) {
+                        Date start = new Date();
+                        //上传json文件
+                        Files.copy(file.toPath(), Paths.get(target+file.getName()), StandardCopyOption.REPLACE_EXISTING);
+                        boolean flag = true;
+                        boolean flag1 = false;
+                        //上传zip文件
+                        List<File> list = new ArrayList<>();
+                        if (isZip) {
+                            ZipFile zipFile = new ZipFile(syncFile.getPath());
+                            if (zipFile.isSplitArchive()) {
+                                //如果是分卷压缩文件,则需要读取所有文件名上传
+                                list = zipFile.getSplitZipFiles();
+                                for (File f : list) {
+                                    Files.copy(f.toPath(), Paths.get(target+f.getName()), StandardCopyOption.REPLACE_EXISTING);
+                                    flag1 = true;
+                                }
+                            } else {
+                                Files.copy(zipFile.getFile().toPath(), Paths.get(target+zipFile.getFile().getName()), StandardCopyOption.REPLACE_EXISTING);
+                                flag1 = true;
+                            }
+                        } else {
+                            flag1 = true;
+                            Files.copy(syncFile.toPath(), Paths.get(target+syncFile.getName()), StandardCopyOption.REPLACE_EXISTING);
+                        }
+
+                        Date end = new Date();
+
+                        String status = "N";
+                        if (flag && flag1) {
+                            //移动文件到备份目录
+                            Files.move(file.toPath(), Paths.get(bakDir + file.getName()), StandardCopyOption.REPLACE_EXISTING);
+                            Files.move(syncFile.toPath(), Paths.get(bakDir + syncFile.getName()), StandardCopyOption.REPLACE_EXISTING);
+                            for (File f : list) {
+                                Files.move(f.toPath(), Paths.get(bakDir + f.getName()), StandardCopyOption.REPLACE_EXISTING);
+                            }
+                            status = "Y";
+                        }
+
+                        OutSyncInfo info = new OutSyncInfo();
+                        info.setSyncType(desc.getType().toString());
+                        info.setCostTime(end.getTime() - start.getTime());
+                        info.setSyncFileMd5(desc.getMd5());
+                        info.setSyncFileName(desc.getName());
+                        info.setSyncSort(sort);
+                        info.setSyncFileSize(file.length());
+                        info.setUploadTime(start);
+                        info.setUploadStatus(status);
+                        syncInfoService.insertOutSyncInfo(info);
+                    } else {
+                        log.error("MD5校验未成功,暂时不上传" + desc.toString());
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log.error("上传出错啦{}", e.getMessage());
+            e.printStackTrace();
+        }
+    }
+
+    /**
      * 上传文件到光闸FTP
      */
     @Override
@@ -165,6 +260,7 @@ public class OutProcessServiceImpl extends OutBaseService implements IOutProcess
 
     /**
      * 删除过期文件
+     *
      * @param day 指定过期天数
      */
     @Override

+ 1 - 1
sync-out/src/main/java/com/jjt/out/task/OutProcessTask.java

@@ -17,7 +17,7 @@ public class OutProcessTask {
     private IOutProcessService processService;
 
     public void sync() {
-        processService.sync();
+        processService.copy();
     }
 
     public void clean(Integer day) {