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

ftp下载改为sftp下载

tl 7 hónapja
szülő
commit
61b0188d30

+ 9 - 0
cpp-admin/pom.xml

@@ -80,6 +80,15 @@
             <artifactId>hutool-all</artifactId>
             <version>5.8.12</version>
         </dependency>
+
+        <!-- SSH安全连接所使用的类库 -->
+        <dependency>
+            <groupId>com.jcraft</groupId>
+            <artifactId>jsch</artifactId>
+            <version>0.1.55</version>
+            <scope>compile</scope>
+            <optional>true</optional>
+        </dependency>
     </dependencies>
     <profiles>    <!--考虑到window 和linux环境 npm命令格式的问题,使用maven的profile实现动态指定命令-->
         <profile>

+ 5 - 6
cpp-admin/src/main/java/com/cpp/web/controller/configManager/ElectricFieldController.java

@@ -1,12 +1,11 @@
 package com.cpp.web.controller.configManager;
 
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.cpp.common.core.domain.R;
-import com.cpp.web.domain.datafactory.FtpChannel;
+import com.cpp.web.domain.datafactory.SftpChannel;
 import com.cpp.web.domain.station.ElectricField;
 import com.cpp.web.domain.station.enums.ProvinceEnum;
-import com.cpp.web.service.datafactory.FtpChannelService;
+import com.cpp.web.service.datafactory.SftpChannelService;
 import com.cpp.web.service.station.ElectricFieldService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -34,7 +33,7 @@ public class ElectricFieldController {
     @Autowired
     ElectricFieldService electricFieldService;
     @Autowired
-    FtpChannelService ftpChannelService;
+    SftpChannelService sftpChannelService;
 
     /**
      * 根据场站名称或者类型分页查询
@@ -122,9 +121,9 @@ public class ElectricFieldController {
 
     @GetMapping("/getFtpChannelId")
     public R getFtpChannelId() {
-        List<FtpChannel> ftpChannelList = ftpChannelService.list();
+        List<SftpChannel> sftpChannelList = sftpChannelService.list();
         List<Map<String, String>> list = new ArrayList<>();
-        for (FtpChannel e : ftpChannelList) {
+        for (SftpChannel e : sftpChannelList) {
             Map<String, String> map = new HashMap<>();
             map.put("label", e.getName());
             map.put("value", e.getId()+"");

+ 11 - 12
cpp-admin/src/main/java/com/cpp/web/controller/configManager/FtpChannelController.java

@@ -1,10 +1,9 @@
 package com.cpp.web.controller.configManager;
 
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.cpp.common.core.domain.R;
-import com.cpp.web.domain.datafactory.FtpChannel;
-import com.cpp.web.service.datafactory.FtpChannelService;
+import com.cpp.web.domain.datafactory.SftpChannel;
+import com.cpp.web.service.datafactory.SftpChannelService;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -18,19 +17,19 @@ import java.util.List;
 @AllArgsConstructor
 public class FtpChannelController {
 
-    private final FtpChannelService ftpChannelService;
+    private final SftpChannelService sftpChannelService;
 
 
     @ApiOperation(value = "添加ftp通道", notes = "添加ftp通道")
     @PostMapping()
-    public Boolean add(@RequestBody FtpChannel ftpChannel) {
-        return ftpChannelService.save(ftpChannel);
+    public Boolean add(@RequestBody SftpChannel sftpChannel) {
+        return sftpChannelService.save(sftpChannel);
     }
 
     @ApiOperation(value = "修改ftp通道", notes = "修改ftp通道")
     @PutMapping()
-    public Boolean update(@RequestBody FtpChannel ftpChannel) {
-        return ftpChannelService.updateById(ftpChannel);
+    public Boolean update(@RequestBody SftpChannel sftpChannel) {
+        return sftpChannelService.updateById(sftpChannel);
 
     }
 
@@ -38,14 +37,14 @@ public class FtpChannelController {
     @ApiOperation(value = "根据id删除ftp通道", notes = "根据id删除ftp通道")
     @DeleteMapping("/{id}")
     public Boolean delete(@PathVariable String id) {
-        return ftpChannelService.removeById(id);
+        return sftpChannelService.removeById(id);
 
     }
 
     @ApiOperation(value = "查询所有ftp通道", notes = "查询所有ftp通道")
     @GetMapping()
-    public List<FtpChannel> getAll() {
-         return ftpChannelService.list();
+    public List<SftpChannel> getAll() {
+         return sftpChannelService.list();
     }
 
     @ApiOperation(value = "分页查询", notes = "分页查询")
@@ -53,6 +52,6 @@ public class FtpChannelController {
     public R page(Long currentPage,Long pageSize) {
         Page page = new Page(currentPage, pageSize);
         page.setMaxLimit((long) -1);
-        return R.ok(ftpChannelService.page(page));
+        return R.ok(sftpChannelService.page(page));
     }
 }

+ 3 - 3
cpp-admin/src/main/java/com/cpp/web/domain/datafactory/FtpChannel.java → cpp-admin/src/main/java/com/cpp/web/domain/datafactory/SftpChannel.java

@@ -22,10 +22,10 @@ import java.util.Date;
  * @author tl
  * @version 1.0
  */
-@TableName("cpp_ftp_channel")
-@ApiModel(value = "cpp_ftp_channel")
+@TableName("cpp_sftp_channel")
+@ApiModel(value = "cpp_sftp_channel")
 @Data
-public class FtpChannel implements Serializable {
+public class SftpChannel implements Serializable {
 
     /**
      * Id

+ 2 - 2
cpp-admin/src/main/java/com/cpp/web/mapper/datafactory/FtpChannelMapper.java → cpp-admin/src/main/java/com/cpp/web/mapper/datafactory/SftpChannelMapper.java

@@ -2,7 +2,7 @@ package com.cpp.web.mapper.datafactory;
 
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.cpp.web.domain.datafactory.FtpChannel;
+import com.cpp.web.domain.datafactory.SftpChannel;
 import org.apache.ibatis.annotations.Mapper;
 
 /**
@@ -12,6 +12,6 @@ import org.apache.ibatis.annotations.Mapper;
  * @date 2022-05-11 09:47:21
  */
 @Mapper
-public interface FtpChannelMapper extends BaseMapper<FtpChannel> {
+public interface SftpChannelMapper extends BaseMapper<SftpChannel> {
 
 }

+ 0 - 225
cpp-admin/src/main/java/com/cpp/web/service/datafactory/FtpFileParsing.java

@@ -1,225 +0,0 @@
-package com.cpp.web.service.datafactory;
-
-import cn.hutool.extra.ftp.Ftp;
-import cn.hutool.extra.ftp.FtpConfig;
-import cn.hutool.extra.ftp.FtpMode;
-import com.cpp.web.domain.AbnormalAlarm;
-import com.cpp.web.domain.datafactory.FtpChannel;
-import com.cpp.web.domain.datafactory.ParsingLog;
-import com.cpp.web.domain.datafactory.ParsingType;
-import com.cpp.web.domain.datafactory.dto.ParsingResultDto;
-import com.cpp.web.domain.enums.AlarmEnum;
-import com.cpp.web.domain.enums.DataSourcesEnum;
-import com.cpp.web.domain.station.ElectricField;
-import com.cpp.web.service.AbnormalAlarmService;
-import com.cpp.web.service.station.ElectricFieldService;
-import com.cpp.web.utils.MessageUtils;
-import lombok.AllArgsConstructor;
-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.scheduling.concurrent.ThreadPoolTaskExecutor;
-import org.springframework.stereotype.Service;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.charset.Charset;
-import java.util.*;
-import java.util.stream.Collectors;
-
-/**
- * ftp文件解析
- *
- * @author tl
- * @date 2022-05-11 09:51:21
- */
-@AllArgsConstructor
-@Slf4j
-@Service
-public class FtpFileParsing {
-
-    private final ParsingTypeService parsingTypeService;
-
-    private final Map<String, ParsingInterface> parsingInterfaceMap;
-
-    private final FtpChannelService ftpChannelService;
-
-    private final ElectricFieldService electricFieldService;
-
-    private final ParsingLogService parsingLogService;
-
-    private final AbnormalAlarmService abnormalAlarmService;
-
-    private final String PARSING_FILE_TEMP_DIR = "/";
-    private final String PARSING_FILE_SUCCESS_DIR = "/";
-    private final String PARSING_FILE_FAIL_DIR = "/";
-
-    private final File fileTempDir = new File(PARSING_FILE_TEMP_DIR);
-
-    private final ThreadPoolTaskExecutor executor;
-
-    /**
-     * 解析文件方法
-     * 逻辑:先配置识别文件名称类型:识别文件名称关键字对应的文件类型
-     * 有文件类型的配置才可以进入下一步
-     * <p>
-     * 查找各场站配置的ftp目录,去下载文件并识别文件类型执行对应的解析策略
-     * <p>
-     * 必要条件:1.配置文件识别类型。2.配置下载路径。3.配置解析公式
-     *
-     * @param
-     * @return
-     */
-    @Scheduled(fixedRate = 300000L)
-    public void parsingFile() {
-
-
-        log.info("-----------------开始执行FTP文件解析任务----------------------");
-
-        List<ParsingType> parsingTypes = parsingTypeService.list();
-
-        if (parsingTypes.size() > 0) {
-
-            List<FtpChannel> channels = ftpChannelService.list();
-
-            List<ElectricField> electricFields = electricFieldService.list();
-            for (FtpChannel channel : channels) {
-
-                List<ElectricField> channelElectricFields = electricFields.stream().filter(e->e.getFtpChanelId()!=null&&e.getFtpChanelId().equals(channel.getId())).collect(Collectors.toList());
-
-                if (channelElectricFields.size() > 0) {
-                    Ftp ftp = createParsingFtp(channel);
-                    if (ftp != null) {
-                        List<ParsingLog> parsingLogs = new ArrayList<>();
-                        List<AbnormalAlarm> abnormalAlarms = new ArrayList<>();
-                        for (ParsingType parsingType : parsingTypes) {
-                            executor.execute(new Runnable() {
-                                @Override
-                                public void run() {
-                                    ParsingInterface parsingInterface = parsingInterfaceMap.get(parsingType.getFileType().name() + "Parsing");
-                                    parsingInterface.activationParsingConf();//初始化解析配置
-                                    for (ElectricField electricField : channelElectricFields) {
-
-                                        String ftpUrl = electricField.getFtpUrl();
-                                        List<String> fileNames = ftp.ls(ftpUrl).stream().filter(f -> f.contains(parsingType.getFileName())).collect(Collectors.toList());
-                                        if (fileNames.size() > 0) {
-                                            for (String fileName : fileNames) {
-                                                try {
-                                                    //下载文件到临时目录
-                                                    ftp.download(ftpUrl, fileName, fileTempDir);
-                                                    File file = FileUtils.getFile(fileTempDir, fileName);
-                                                    ParsingLog parsingLog = new ParsingLog();
-                                                    Date now = new Date();
-                                                    parsingLog.setParsingTime(now);
-                                                    ParsingResultDto parsingResultDto = parsingInterface.parsing(file, electricField.getStationCode());
-                                                    parsingLog.setParsingDescribe(parsingResultDto.getMessage());
-                                                    parsingLog.setFileType(parsingType.getFileType());
-                                                    parsingLog.setDataSources(DataSourcesEnum.E1);
-                                                    if (parsingResultDto.getStatus().equals("fail")) {
-                                                        try {
-                                                            File failFileDir = new File(PARSING_FILE_FAIL_DIR + File.separator + DateFormatUtils.format(now, "yyyy-MM-DD"));
-                                                            File failFile = new File(failFileDir.getPath() + File.separator + fileName);
-                                                            if (failFile.exists()) {
-                                                                failFile.delete();
-                                                                log.error("已有过解析失败文件,错误文件将覆盖!场站编号:{},文件名称:{}", electricField.getStationCode(), fileName);
-                                                            }
-
-                                                            FileUtils.moveFile(file, failFileDir);
-                                                        } catch (IOException e) {
-                                                            log.error("解析文件失败后文件移动失败!场站编号:{},文件名称:{}", electricField.getStationCode(), fileName, e);
-                                                            e.printStackTrace();
-                                                        }
-                                                        file.delete();//失败删除本地文件,等待下次下载
-                                                        parsingLog.setParsingFileStatus("失败");
-
-                                                        abnormalAlarms.add(new AbnormalAlarm(DataSourcesEnum.E3, AlarmEnum.E4, MessageUtils.format("无法解析场站端文件:{}", fileName), electricField.getStationCode()));
-
-                                                    } else {
-                                                        ftp.delFile(ftpUrl + "/" + fileName);//成功删除ftp上的文件
-
-                                                        try {
-                                                            File successFileDir = new File(PARSING_FILE_SUCCESS_DIR + File.separator + DateFormatUtils.format(now, "yyyy-MM-DD"));
-                                                            File successFile = new File(successFileDir.getPath() + File.separator + fileName);
-                                                            if (successFile.exists()) {
-                                                                successFile.delete();
-                                                                log.error("已有过解析成功文件,成功文件将覆盖!场站编号:{},文件名称:{}", electricField.getStationCode(), fileName);
-                                                            }
-
-                                                            FileUtils.moveFile(file, successFileDir);
-                                                        } catch (IOException e) {
-                                                            log.error("解析文件成功后文件移动失败!场站编号:{},文件名称:{}", electricField.getStationCode(), fileName, e);
-                                                            e.printStackTrace();
-                                                        }
-                                                        file.delete();//失败删除本地文件,等待下次下载
-                                                        parsingLog.setParsingFileStatus("成功");
-                                                    }
-                                                    parsingLogs.add(parsingLog);
-                                                } catch (Exception e) {
-                                                    abnormalAlarms.add(new AbnormalAlarm(DataSourcesEnum.E3, AlarmEnum.E4, MessageUtils.format("无法解析场站端文件:{}", fileName), electricField.getStationCode()));
-                                                    log.error("下载并解析文件{}时异常", fileName, e);
-                                                }
-
-                                            }
-                                        }
-                                    }
-                                }
-                            });
-
-                        }
-
-                        parsingLogService.saveBatch(parsingLogs);
-
-                        try {
-                            ftp.close();
-                        } catch (IOException e) {
-                            e.printStackTrace();
-                        }
-
-                        abnormalAlarmService.saveBatch(abnormalAlarms);
-                    }
-
-                }
-            }
-
-
-        } else {
-            log.info("未配置文件识别类型标识,无法进行下载解析");
-        }
-
-        log.info("-----------------执行FTP文件解析任务完成----------------------");
-    }
-
-    public Ftp createParsingFtp(FtpChannel ftpChannel) {
-        Ftp ftp = null;
-        try {
-
-            FtpConfig ftpConfig = new FtpConfig();
-            try {
-                //创建ftp配置
-                ftpConfig.setHost(ftpChannel.getIp());
-                ftpConfig.setPort(ftpChannel.getPort());
-                ftpConfig.setPassword(ftpChannel.getPassword());
-                ftpConfig.setUser(ftpChannel.getUsername());
-                //字符集使用UTF-8
-                ftpConfig.setCharset(Charset.forName("UTF-8"));
-                ftpConfig.setConnectionTimeout(5 * 1000L);
-
-                //设置主动模式连接
-                ftp = new Ftp(ftpConfig, FtpMode.Active);
-                log.info("ip:{} :ftp服务器连接成功,error:", ftpConfig.getHost());
-            } catch (Exception e) {
-                log.error("ip:{} :ftp服务器连接失败,error", ftpConfig.getHost(), e);
-                if (ftp != null) {
-                    ftp.close();
-                }
-            }
-        } catch (Exception ex) {
-            log.error("获取ftp配置失败。", ex);
-        } finally {
-            return ftp;
-        }
-    }
-
-
-}

+ 2 - 2
cpp-admin/src/main/java/com/cpp/web/service/datafactory/FtpChannelService.java → cpp-admin/src/main/java/com/cpp/web/service/datafactory/SftpChannelService.java

@@ -1,7 +1,7 @@
 package com.cpp.web.service.datafactory;
 
 import com.baomidou.mybatisplus.extension.service.IService;
-import com.cpp.web.domain.datafactory.FtpChannel;
+import com.cpp.web.domain.datafactory.SftpChannel;
 
 /**
  * 解析ftp路径业务层接口
@@ -9,6 +9,6 @@ import com.cpp.web.domain.datafactory.FtpChannel;
  * @author tl
  * @date 2022-05-11 09:51:21
  */
-public interface FtpChannelService extends IService<FtpChannel> {
+public interface SftpChannelService extends IService<SftpChannel> {
 
 }

+ 251 - 0
cpp-admin/src/main/java/com/cpp/web/service/datafactory/SftpFileParsing.java

@@ -0,0 +1,251 @@
+package com.cpp.web.service.datafactory;
+
+import cn.hutool.core.util.CharsetUtil;
+import cn.hutool.extra.ftp.Ftp;
+import cn.hutool.extra.ftp.FtpConfig;
+import cn.hutool.extra.ftp.FtpMode;
+import cn.hutool.extra.ssh.JschUtil;
+import cn.hutool.extra.ssh.Sftp;
+import com.cpp.web.domain.AbnormalAlarm;
+import com.cpp.web.domain.datafactory.SftpChannel;
+import com.cpp.web.domain.datafactory.ParsingLog;
+import com.cpp.web.domain.datafactory.ParsingType;
+import com.cpp.web.domain.datafactory.dto.ParsingResultDto;
+import com.cpp.web.domain.enums.AlarmEnum;
+import com.cpp.web.domain.enums.DataSourcesEnum;
+import com.cpp.web.domain.station.ElectricField;
+import com.cpp.web.service.AbnormalAlarmService;
+import com.cpp.web.service.station.ElectricFieldService;
+import com.cpp.web.utils.MessageUtils;
+import com.jcraft.jsch.ChannelSftp;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.Session;
+import lombok.AllArgsConstructor;
+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.scheduling.concurrent.ThreadPoolTaskExecutor;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.nio.charset.Charset;
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * ftp文件解析
+ *
+ * @author tl
+ * @date 2022-05-11 09:51:21
+ */
+@AllArgsConstructor
+@Slf4j
+@Service
+public class SftpFileParsing {
+
+    private final ParsingTypeService parsingTypeService;
+
+    private final Map<String, ParsingInterface> parsingInterfaceMap;
+
+    private final SftpChannelService sftpChannelService;
+
+    private final ElectricFieldService electricFieldService;
+
+    private final ParsingLogService parsingLogService;
+
+    private final AbnormalAlarmService abnormalAlarmService;
+
+    private final String PARSING_FILE_TEMP_DIR = "/";
+    private final String PARSING_FILE_SUCCESS_DIR = "/";
+    private final String PARSING_FILE_FAIL_DIR = "/";
+
+    private final File fileTempDir = new File(PARSING_FILE_TEMP_DIR);
+
+    private final ThreadPoolTaskExecutor executor;
+
+    /**
+     * 解析文件得定时任务
+     * 逻辑:先配置识别文件名称类型:识别文件名称关键字对应的文件类型
+     * 有文件类型的配置才可以进入下一步
+     * <p>
+     * 查找各场站配置的ftp目录,去下载文件并识别文件类型执行对应的解析策略
+     * <p>
+     * 必要条件:1.配置文件识别类型。2.配置下载路径。3.配置解析公式
+     *
+     * @param
+     * @return
+     */
+    @Scheduled(fixedRate = 300000L)
+    public void parsingFileJob() {
+
+
+        log.info("-----------------开始执行FTP文件解析任务----------------------");
+
+        List<ParsingType> parsingTypes = parsingTypeService.list();
+
+        if (parsingTypes.size() > 0) {
+
+            List<SftpChannel> channels = sftpChannelService.list();
+
+            List<ElectricField> electricFields = electricFieldService.list();
+            for (SftpChannel channel : channels) {
+
+                List<ElectricField> channelElectricFields = electricFields.stream().filter(e -> e.getFtpChanelId() != null && e.getFtpChanelId().equals(channel.getId())).collect(Collectors.toList());
+
+                if (channelElectricFields.size() > 0) {
+                    executeSftpParsing(channel, parsingTypes, electricFields);
+                }
+            }
+
+
+        } else {
+            log.info("未配置文件识别类型标识,无法进行下载解析");
+        }
+
+        log.info("-----------------执行FTP文件解析任务完成----------------------");
+    }
+
+    /**
+     * sftp下载文件并解析文件的具体步骤
+     * @param parsingTypes
+     * @param channelElectricFields
+     * @param sftp
+     */
+    private void parsingFile(List<ParsingType> parsingTypes, List<ElectricField> channelElectricFields, Sftp sftp) {
+        List<ParsingLog> parsingLogs = new ArrayList<>();
+        List<AbnormalAlarm> abnormalAlarms = new ArrayList<>();
+        for (ParsingType parsingType : parsingTypes) {
+            executor.execute(new Runnable() {
+                @Override
+                public void run() {
+                    ParsingInterface parsingInterface = parsingInterfaceMap.get(parsingType.getFileType().name() + "Parsing");
+                    parsingInterface.activationParsingConf();//初始化解析配置
+                    for (ElectricField electricField : channelElectricFields) {
+
+                        String ftpUrl = electricField.getFtpUrl();
+                        List<String> fileNames = sftp.ls(ftpUrl).stream().filter(f -> f.contains(parsingType.getFileName())).collect(Collectors.toList());
+                        if (fileNames.size() > 0) {
+                            for (String fileName : fileNames) {
+                                try {
+                                    //下载文件到临时目录
+                                    sftp.download(ftpUrl, fileTempDir,fileName);
+                                    File file = FileUtils.getFile(fileTempDir, fileName);
+                                    ParsingLog parsingLog = new ParsingLog();
+                                    Date now = new Date();
+                                    parsingLog.setParsingTime(now);
+                                    ParsingResultDto parsingResultDto = parsingInterface.parsing(file, electricField.getStationCode());
+                                    parsingLog.setParsingDescribe(parsingResultDto.getMessage());
+                                    parsingLog.setFileType(parsingType.getFileType());
+                                    parsingLog.setDataSources(DataSourcesEnum.E1);
+                                    if (parsingResultDto.getStatus().equals("fail")) {
+                                        try {
+                                            File failFileDir = new File(PARSING_FILE_FAIL_DIR + File.separator + DateFormatUtils.format(now, "yyyy-MM-DD"));
+                                            File failFile = new File(failFileDir.getPath() + File.separator + fileName);
+                                            if (failFile.exists()) {
+                                                failFile.delete();
+                                                log.error("已有过解析失败文件,错误文件将覆盖!场站编号:{},文件名称:{}", electricField.getStationCode(), fileName);
+                                            }
+
+                                            FileUtils.moveFile(file, failFileDir);
+                                        } catch (IOException e) {
+                                            log.error("解析文件失败后文件移动失败!场站编号:{},文件名称:{}", electricField.getStationCode(), fileName, e);
+                                            e.printStackTrace();
+                                        }
+                                        file.delete();//失败删除本地文件,等待下次下载
+                                        parsingLog.setParsingFileStatus("失败");
+
+                                        abnormalAlarms.add(new AbnormalAlarm(DataSourcesEnum.E3, AlarmEnum.E4, MessageUtils.format("无法解析场站端文件:{}", fileName), electricField.getStationCode()));
+
+                                    } else {
+                                        sftp.delFile(ftpUrl + "/" + fileName);//成功删除ftp上的文件
+
+                                        try {
+                                            File successFileDir = new File(PARSING_FILE_SUCCESS_DIR + File.separator + DateFormatUtils.format(now, "yyyy-MM-DD"));
+                                            File successFile = new File(successFileDir.getPath() + File.separator + fileName);
+                                            if (successFile.exists()) {
+                                                successFile.delete();
+                                                log.error("已有过解析成功文件,成功文件将覆盖!场站编号:{},文件名称:{}", electricField.getStationCode(), fileName);
+                                            }
+
+                                            FileUtils.moveFile(file, successFileDir);
+                                        } catch (IOException e) {
+                                            log.error("解析文件成功后文件移动失败!场站编号:{},文件名称:{}", electricField.getStationCode(), fileName, e);
+                                            e.printStackTrace();
+                                        }
+                                        file.delete();//失败删除本地文件,等待下次下载
+                                        parsingLog.setParsingFileStatus("成功");
+                                    }
+                                    parsingLogs.add(parsingLog);
+                                } catch (Exception e) {
+                                    abnormalAlarms.add(new AbnormalAlarm(DataSourcesEnum.E3, AlarmEnum.E4, MessageUtils.format("无法解析场站端文件:{}", fileName), electricField.getStationCode()));
+                                    log.error("下载并解析文件{}时异常", fileName, e);
+                                }
+
+                            }
+                        }
+                    }
+                }
+            });
+
+        }
+
+        parsingLogService.saveBatch(parsingLogs);
+
+        abnormalAlarmService.saveBatch(abnormalAlarms);
+
+    }
+
+
+    /**
+     * 创建sftp 并解析
+     *
+     * @param sftpChannel
+     * @param parsingTypes
+     * @param channelElectricFields
+     */
+    public void executeSftpParsing(SftpChannel sftpChannel, List<ParsingType> parsingTypes, List<ElectricField> channelElectricFields) {
+        // SFTP方式上报
+        Sftp sftp = null;
+        JSch jsch = new JSch();
+        ChannelSftp channel = null;
+        Session sshSession = null;
+        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);
+            }
+
+        } finally {
+            if (sftp != null) {
+                log.info("sftp客户端关闭");
+                JschUtil.closeAll();
+                sftp.close();
+                channel.quit();
+                channel.disconnect();
+                sshSession.disconnect();
+            }
+        }
+    }
+
+}

+ 0 - 18
cpp-admin/src/main/java/com/cpp/web/service/datafactory/impl/FtpChannelServiceImpl.java

@@ -1,18 +0,0 @@
-package com.cpp.web.service.datafactory.impl;
-
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.cpp.web.domain.datafactory.FtpChannel;
-import com.cpp.web.mapper.datafactory.FtpChannelMapper;
-import com.cpp.web.service.datafactory.FtpChannelService;
-import org.springframework.stereotype.Service;
-
-/**
- * 解析ftp路径业务层实现类
- *
- * @author tl
- * @date 2022-05-11 18:07:03
- */
-@Service
-public class FtpChannelServiceImpl extends ServiceImpl<FtpChannelMapper, FtpChannel> implements FtpChannelService {
-
-}

+ 18 - 0
cpp-admin/src/main/java/com/cpp/web/service/datafactory/impl/SftpChannelServiceImpl.java

@@ -0,0 +1,18 @@
+package com.cpp.web.service.datafactory.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.cpp.web.domain.datafactory.SftpChannel;
+import com.cpp.web.mapper.datafactory.SftpChannelMapper;
+import com.cpp.web.service.datafactory.SftpChannelService;
+import org.springframework.stereotype.Service;
+
+/**
+ * 解析ftp路径业务层实现类
+ *
+ * @author tl
+ * @date 2022-05-11 18:07:03
+ */
+@Service
+public class SftpChannelServiceImpl extends ServiceImpl<SftpChannelMapper, SftpChannel> implements SftpChannelService {
+
+}

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

@@ -151,7 +151,7 @@
             <el-form-item label="ftp通道名称">
               <el-select v-model="form.ftpChanelId" placeholder="请选择" style="width: 100%" clearable @change="ftpChannelChange">
                 <el-option
-                  v-for="item in this.ftpChannelList"
+                  v-for="item in this.sftpChannelList"
                   :key="item.value"
                   :label="item.label"
                   :value="item.value"
@@ -182,7 +182,7 @@ export default {
   name: 'inverterinfo',
   data() {
     return {
-      ftpChannelList:[],
+      sftpChannelList:[],
       provinceEnumList:[],
       form: {
         stationCode: '',
@@ -254,7 +254,7 @@ export default {
     })
     // 获取后端ftp通道
     this.$axios.get('/electricfield/getFtpChannelId').then(response => {
-      this.ftpChannelList = response.data
+      this.sftpChannelList = response.data
     })
   },
   mounted() {
@@ -423,7 +423,7 @@ export default {
     },
     ftpChanelIdFormat({cellValue, row, column}) {
       if (cellValue != undefined && cellValue != ''){
-        const item = this.ftpChannelList.find(item => item.value === cellValue.toString())
+        const item = this.sftpChannelList.find(item => item.value === cellValue.toString())
         return item ? item.label : ''
       }
     },

+ 5 - 5
cpp-ui/src/views/configManager/ftpChannel/index.vue

@@ -81,7 +81,7 @@
 
 <script>
 export default {
-  name: 'ftpChannel',
+  name: 'sftpChannel',
   data() {
     return {
       form: {},
@@ -122,7 +122,7 @@ export default {
       //   this.searchForm = {delFlag: 0}
       // }
       this.tableLoading = true
-      this.$axios.get("/ftpChannel/page", {
+      this.$axios.get("/sftpChannel/page", {
         params: {
           currentPage: page.currentPage,
           pageSize: page.pageSize
@@ -170,7 +170,7 @@ export default {
         cancelButtonText: '取消',
         type: 'warning'
       }).then(function () {
-        return this.$axios.delete("/ftpChannel/" + row.id)
+        return this.$axios.delete("/sftpChannel/" + row.id)
       }).then(data => {
         this.$message.success('删除成功')
         this.getList(this.page)
@@ -180,7 +180,7 @@ export default {
       if (this.dialogType === 'add') {
         // row.cid = this.channelId
         // row.stationCode = this.stationCode
-        this.$axios.post("/ftpChannel", this.form).then(data => {
+        this.$axios.post("/sftpChannel", this.form).then(data => {
           this.$message.success('添加成功')
           this.getList()
           this.dialogVisible = false
@@ -188,7 +188,7 @@ export default {
           console.log('添加失败:' + e)
         });
       } else {
-        this.$axios.put("/ftpChannel", this.form).then(data => {
+        this.$axios.put("/sftpChannel", this.form).then(data => {
           this.$message.success('修改成功')
           this.getList()
           this.dialogVisible = false