소스 검색

sftp增加历史文件打包上传备份至服务器,本地清理功能

tl 6 달 전
부모
커밋
de3f4a9ca0

+ 2 - 2
cpp-admin/src/main/java/com/cpp/web/controller/tool/EnumSelectController.java

@@ -89,8 +89,8 @@ public class EnumSelectController {
     }
 
 
-    @GetMapping("/pquipmentTypeEnum")
-    public R EquipmentTypeEnum() {
+    @GetMapping("/equipmentTypeEnum")
+    public R equipmentTypeEnum() {
 
         List<EnumSelect> list = new ArrayList<>();
         for (EquipmentTypeEnum value : EquipmentTypeEnum.values()) {

+ 124 - 41
cpp-admin/src/main/java/com/cpp/web/service/datafactory/SftpFileParsing.java

@@ -13,21 +13,23 @@ import com.cpp.web.domain.station.ElectricField;
 import com.cpp.web.service.station.ElectricFieldService;
 import com.cpp.web.utils.LogUtil;
 import com.cpp.web.utils.ParsingFileUtil;
+import com.cpp.web.utils.ZipUtil;
 import com.jcraft.jsch.ChannelSftp;
 import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.Session;
 import lombok.AllArgsConstructor;
+import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.time.DateFormatUtils;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
-import java.io.File;
-import java.io.IOException;
+import java.io.*;
 import java.lang.reflect.Field;
 import java.util.*;
 import java.util.stream.Collectors;
+import java.util.zip.ZipOutputStream;
 
 /**
  * ftp文件解析
@@ -54,6 +56,7 @@ public class SftpFileParsing {
     private final File PARSING_FILE_SUCCESS_DIR = ParsingFileUtil.checkGetPath(ParsingFileUtil.getSftpDownload() + File.separator + "success");
     private final File PARSING_FILE_FAIL_DIR = ParsingFileUtil.checkGetPath(ParsingFileUtil.getSftpDownload() + File.separator + "fail");
 
+    private final String CHARSET_NAME = "UTF-8";
 //    private AlarmLog alarmLog = AlarmLog.getInstance();
 //    private final ThreadPoolTaskExecutor executor;
 
@@ -71,26 +74,29 @@ public class SftpFileParsing {
      */
     public void parsingFile() {
 
-
         log.info("-----------------开始执行FTP文件解析任务----------------------");
 
         List<ParsingType> parsingTypes = parsingTypeService.list();
 
         if (parsingTypes.size() > 0) {
 
-            List<SftpChannel> channels = sftpChannelService.list();
+            List<SftpChannel> sftpChannels = sftpChannelService.list();
 
             List<ElectricField> electricFields = electricFieldService.list();
-            for (SftpChannel channel : channels) {
+            for (SftpChannel sftpChannel : sftpChannels) {
 
-                List<ElectricField> channelElectricFields = electricFields.stream().filter(e -> e.getSftpChanelId() != null && e.getSftpChanelId().equals(channel.getId())).collect(Collectors.toList());
+                List<ElectricField> channelElectricFields = electricFields.stream().filter(e -> e.getSftpChanelId() != null && e.getSftpChanelId().equals(sftpChannel.getId())).collect(Collectors.toList());
 
                 if (channelElectricFields.size() > 0) {
-                    executeSftpParsing(channel, parsingTypes, electricFields);
-                }
-            }
 
 
+                    SftpConfig sftpConfig = createSftp(sftpChannel);
+                    if (sftpConfig.getSftp() != null) {
+                        parsingFile(parsingTypes, channelElectricFields, sftpConfig.getSftp());
+                        sftpConfig.close();
+                    }
+                }
+            }
         } else {
             log.info("未配置文件识别类型标识,无法进行下载解析");
         }
@@ -136,7 +142,7 @@ public class SftpFileParsing {
                             parsingLog.setStationCode(electricField.getStationCode());
                             if (parsingResultDto.getStatus().equals("fail")) {
                                 try {
-                                    File failFileDir = new File(PARSING_FILE_FAIL_DIR.getPath() + File.separator + DateFormatUtils.format(now, "yyyy-MM-dd"));
+                                    File failFileDir = new File(PARSING_FILE_FAIL_DIR.getPath() + File.separator + DateFormatUtils.format(now, "yyyy-MM-dd") + File.separator + electricField.getStationCode());
                                     File failFile = new File(failFileDir.getPath() + File.separator + fileName);
                                     if (failFile.exists()) {
                                         failFile.delete();
@@ -158,7 +164,7 @@ public class SftpFileParsing {
                                 sftp.delFile(ftpUrl + "/" + fileName);//成功删除ftp上的文件
 
                                 try {
-                                    File successFileDir = new File(PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + DateFormatUtils.format(now, "yyyy-MM-dd"));
+                                    File successFileDir = new File(PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + DateFormatUtils.format(now, "yyyy-MM-dd") + File.separator + electricField.getStationCode());
                                     File successFile = new File(successFileDir.getPath() + File.separator + fileName);
                                     if (successFile.exists()) {
                                         successFile.delete();
@@ -195,41 +201,118 @@ public class SftpFileParsing {
      * 创建sftp 并解析
      *
      * @param sftpChannel
-     * @param parsingTypes
-     * @param channelElectricFields
      */
-    public void executeSftpParsing(SftpChannel sftpChannel, List<ParsingType> parsingTypes, List<ElectricField> channelElectricFields) {
+    public SftpConfig createSftp(SftpChannel sftpChannel) {
         // SFTP方式上报
-        Sftp sftp = null;
-        JSch jsch = new JSch();
-        ChannelSftp channel = null;
-        Session sshSession = null;
+        SftpConfig sftpConfig = new SftpConfig();
+
         try {
-            try {
-                sshSession = jsch.getSession(sftpChannel.getUsername(), sftpChannel.getIp(), sftpChannel.getPort());
-                sshSession.setPassword(sftpChannel.getPassword());
-                sshSession.setTimeout(30000);
-                Properties sshConfig = new Properties();
-                sshConfig.put("StrictHostKeyChecking", "no");
-                sshSession.setConfig(sshConfig);
-                log.info("sftp开始连接...");
-                sshSession.connect();
-                channel = (ChannelSftp) sshSession.openChannel("sftp");
-                channel.connect();
-                Class cl = ChannelSftp.class;
-                Field f = cl.getDeclaredField("server_version");
-                f.setAccessible(true);
-                f.set(channel, 2);
-                channel.setFilenameEncoding("UTF-8");
-                sftp = new Sftp(channel, CharsetUtil.CHARSET_UTF_8);
-
-                parsingFile(parsingTypes, channelElectricFields, sftp);
-            } catch (Exception e) {
-                log.error("sftp连接异常:" + e);
+            sftpConfig.setSshSession(sftpConfig.getJsch().getSession(sftpChannel.getUsername(), sftpChannel.getIp(), sftpChannel.getPort()));
+            sftpConfig.getSshSession().setPassword(sftpChannel.getPassword());
+            sftpConfig.getSshSession().setTimeout(30000);
+            Properties sshConfig = new Properties();
+            sshConfig.put("StrictHostKeyChecking", "no");
+            sftpConfig.getSshSession().setConfig(sshConfig);
+            log.info("sftp开始连接...");
+            sftpConfig.getSshSession().connect();
+            sftpConfig.setChannel((ChannelSftp)sftpConfig.getSshSession().openChannel("sftp"));
+            sftpConfig.getChannel().connect();
+            Class cl = ChannelSftp.class;
+            Field f = cl.getDeclaredField("server_version");
+            f.setAccessible(true);
+            f.set(sftpConfig.getChannel(), 2);
+            sftpConfig.getChannel().setFilenameEncoding(CHARSET_NAME);
+            sftpConfig.setSftp(new Sftp(sftpConfig.getChannel(), CharsetUtil.CHARSET_UTF_8));
+        } catch (Exception e) {
+            log.error("sftp连接异常:" + e);
+            if (sftpConfig.getSftp() != null) {
+                sftpConfig.close();
             }
+        }
+        return sftpConfig;
+    }
+
+
+
+
+    /**
+     * 压缩前一日解析文件并上传至sftp服务器,本地不保留
+     */
+    public void uploadZipFile() {
+        List<SftpChannel> sftpChannels = sftpChannelService.list();
+
+        List<ElectricField> electricFields = electricFieldService.list();
+
+        String dirYesToday = DateFormatUtils.format(new Date(System.currentTimeMillis() - 86400000L), "yyyy-MM-dd");
+        for (SftpChannel sftpChannel : sftpChannels) {
+
+            List<ElectricField> channelElectricFields = electricFields.stream().filter(e -> e.getSftpChanelId() != null && e.getSftpChanelId().equals(sftpChannel.getId())).collect(Collectors.toList());
+
+            if (channelElectricFields.size() > 0) {
+                SftpConfig sftpConfig = createSftp(sftpChannel);
+                if (sftpConfig.getSftp() != null) {
+                    for (ElectricField channelElectricField : channelElectricFields) {
+                        File failFileDir = new File(PARSING_FILE_FAIL_DIR.getPath() + File.separator + dirYesToday + File.separator + channelElectricField.getStationCode());
+                        if (failFileDir.exists()) {
+                            String zipPath = ZipUtil.zip(failFileDir.getAbsolutePath(), PARSING_FILE_FAIL_DIR.getPath() + File.separator + dirYesToday+"_SUCCESS.zip");
+                            if (zipPath != null && new File(zipPath).exists()) {
+                                upload(sftpConfig.getSftp(), channelElectricField.getSftpBackupUrl(), zipPath);
+                            }
+                        }
+
+                        File successFileDir = new File(PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + dirYesToday + File.separator + channelElectricField.getStationCode());
+                        if (successFileDir.exists()) {
+                            String zipPath = ZipUtil.zip(successFileDir.getAbsolutePath(), PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + dirYesToday+"_SUCCESS.zip");
+                            if (zipPath != null && new File(zipPath).exists()) {
+                                upload(sftpConfig.getSftp(), channelElectricField.getSftpBackupUrl(), zipPath);
+                            }
+                        }
+                    }
+                    sftpConfig.close();
+                }
+            }
+        }
+    }
+
+    /**
+     * 上传文件
+     *
+     * @param sftp
+     * @param destPathName
+     * @param srcName
+     */
+    private void upload(Sftp sftp, String destPathName, String srcName) {
+        FileInputStream fis = null;
+        try {
+            fis = new FileInputStream(srcName);
+            File file = new File(srcName);
+            sftp.getClient().put(fis, destPathName + "/" + file.getName());
+            file.delete();
+        } catch (Exception e) {
+            log.error("上传备份文件异常",e);
 
         } finally {
-            if (sftp != null) {
+            try {
+                if (fis != null) {
+                    fis.close();
+                }
+            } catch (IOException e) {
+                log.error("关闭文件流异常", e);
+            }
+        }
+    }
+
+
+
+    @Data
+    public class SftpConfig{
+        private JSch jsch = new JSch();
+        private ChannelSftp channel = null;
+        private Session sshSession = null;
+        private Sftp sftp = null;
+
+        public void close() {
+            if (this.sftp != null) {
                 log.info("sftp客户端关闭");
                 JschUtil.closeAll();
                 sftp.close();
@@ -238,6 +321,6 @@ public class SftpFileParsing {
                 sshSession.disconnect();
             }
         }
-    }
 
+    }
 }

+ 4 - 4
cpp-ui/src/views/configManager/electricField/index.vue

@@ -78,9 +78,9 @@
         <vxe-table-column field="longitude" title="经度"></vxe-table-column>
         <vxe-table-column field="latitude" title="纬度"></vxe-table-column>
         <vxe-table-column field="provinceEnum" title="省份" :formatter="provinceEnumFormat"></vxe-table-column>
-        <vxe-table-column field="ftpChanelId" title="ftpt通道名称" :formatter="ftpChanelIdFormat"></vxe-table-column>
-        <vxe-table-column field="ftpUrl" title="ftp扫描路径"></vxe-table-column>
-        <vxe-table-column field="ftpBackupUrl" title="ftp备份路径"></vxe-table-column>
+        <vxe-table-column field="sftpChanelId" title="ftpt通道名称" :formatter="ftpChanelIdFormat"></vxe-table-column>
+        <vxe-table-column field="sftpUrl" title="ftp扫描路径"></vxe-table-column>
+        <vxe-table-column field="sftpBackupUrl" title="ftp备份路径"></vxe-table-column>
       </vxe-table>
       <vxe-pager
         background
@@ -196,7 +196,7 @@
 
 <script>
 export default {
-  name: 'inverterinfo',
+  name: 'electricField',
   data() {
     return {
       sftpChannelList: [],