|
@@ -13,20 +13,16 @@ import com.cpp.web.domain.station.ElectricField;
|
|
import com.cpp.web.service.station.ElectricFieldService;
|
|
import com.cpp.web.service.station.ElectricFieldService;
|
|
import com.cpp.web.utils.LogUtil;
|
|
import com.cpp.web.utils.LogUtil;
|
|
import com.cpp.web.utils.ParsingFileUtil;
|
|
import com.cpp.web.utils.ParsingFileUtil;
|
|
|
|
+import com.cpp.web.utils.SftpUtil;
|
|
import com.cpp.web.utils.ZipUtil;
|
|
import com.cpp.web.utils.ZipUtil;
|
|
-import com.jcraft.jsch.ChannelSftp;
|
|
|
|
-import com.jcraft.jsch.JSch;
|
|
|
|
-import com.jcraft.jsch.Session;
|
|
|
|
|
|
+import com.cpp.web.utils.sftp.SftpTool;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
-import lombok.Data;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
-import java.lang.reflect.Field;
|
|
|
|
-import java.nio.file.Files;
|
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -55,7 +51,6 @@ public class SftpFileParsing {
|
|
private final File PARSING_FILE_SUCCESS_DIR = ParsingFileUtil.checkGetPath(ParsingFileUtil.getSftpDownload() + File.separator + "success");
|
|
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 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 AlarmLog alarmLog = AlarmLog.getInstance();
|
|
// private final ThreadPoolTaskExecutor executor;
|
|
// private final ThreadPoolTaskExecutor executor;
|
|
|
|
|
|
@@ -87,10 +82,10 @@ public class SftpFileParsing {
|
|
List<ElectricField> channelElectricFields = electricFields.stream().filter(e -> e.getSftpChanelId() != null && e.getSftpChanelId().equals(sftpChannel.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) {
|
|
if (channelElectricFields.size() > 0) {
|
|
- SftpConfig sftpConfig = createSftp(sftpChannel);
|
|
|
|
- if (sftpConfig.getSftp() != null) {
|
|
|
|
- parsingFile(parsingTypes, channelElectricFields, sftpConfig.getSftp());
|
|
|
|
- sftpConfig.close();
|
|
|
|
|
|
+ SftpTool sftpTool = SftpUtil.createSftp(sftpChannel);
|
|
|
|
+ if (sftpTool.getSftp() != null) {
|
|
|
|
+ parsingFile(parsingTypes, channelElectricFields, sftpTool.getSftp());
|
|
|
|
+ sftpTool.close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -190,43 +185,7 @@ public class SftpFileParsing {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * 创建sftp 并解析
|
|
|
|
- *
|
|
|
|
- * @param sftpChannel sftp通道信息
|
|
|
|
- * @return sftp控制
|
|
|
|
- */
|
|
|
|
- public SftpConfig createSftp(
|
|
|
|
- SftpChannel sftpChannel
|
|
|
|
- ) {
|
|
|
|
- // SFTP方式上报
|
|
|
|
- SftpConfig sftpConfig = new SftpConfig();
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
- 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;
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -242,8 +201,8 @@ public class SftpFileParsing {
|
|
for (SftpChannel sftpChannel : sftpChannels) {
|
|
for (SftpChannel sftpChannel : sftpChannels) {
|
|
List<ElectricField> channelElectricFields = electricFields.stream().filter(e -> e.getSftpChanelId() != null && e.getSftpChanelId().equals(sftpChannel.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) {
|
|
if (channelElectricFields.size() > 0) {
|
|
- SftpConfig sftpConfig = createSftp(sftpChannel);
|
|
|
|
- if (sftpConfig.getSftp() != null) {
|
|
|
|
|
|
+ SftpTool sftpTool = SftpUtil.createSftp(sftpChannel);
|
|
|
|
+ if (sftpTool.getSftp() != null) {
|
|
String failSrc = PARSING_FILE_FAIL_DIR.getPath() + File.separator + dirYesToday;
|
|
String failSrc = PARSING_FILE_FAIL_DIR.getPath() + File.separator + dirYesToday;
|
|
String successSrc = PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + dirYesToday;
|
|
String successSrc = PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + dirYesToday;
|
|
for (ElectricField channelElectricField : channelElectricFields) {
|
|
for (ElectricField channelElectricField : channelElectricFields) {
|
|
@@ -252,7 +211,7 @@ public class SftpFileParsing {
|
|
String zipFileName = dirYesToday + "_" + channelElectricField.getStationCode() + "_FAIL.zip";
|
|
String zipFileName = dirYesToday + "_" + channelElectricField.getStationCode() + "_FAIL.zip";
|
|
String zipPath = PARSING_FILE_FAIL_DIR.getPath() + File.separator + zipFileName;
|
|
String zipPath = PARSING_FILE_FAIL_DIR.getPath() + File.separator + zipFileName;
|
|
if (ZipUtil.zip(failFileDir.getAbsolutePath(), zipPath) && new File(zipPath).exists()) {
|
|
if (ZipUtil.zip(failFileDir.getAbsolutePath(), zipPath) && new File(zipPath).exists()) {
|
|
- upload(sftpConfig.getSftp(), channelElectricField.getSftpBackupUrl(), zipPath, zipFileName);
|
|
|
|
|
|
+ SftpUtil.upload(sftpTool.getSftp(), channelElectricField.getSftpBackupUrl(), zipPath, zipFileName,true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
File successFileDir = new File(successSrc + File.separator + channelElectricField.getStationCode());
|
|
File successFileDir = new File(successSrc + File.separator + channelElectricField.getStationCode());
|
|
@@ -260,60 +219,18 @@ public class SftpFileParsing {
|
|
String zipFileName = dirYesToday + "_" + channelElectricField.getStationCode() + "_SUCCESS.zip";
|
|
String zipFileName = dirYesToday + "_" + channelElectricField.getStationCode() + "_SUCCESS.zip";
|
|
String zipPath = PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + zipFileName;
|
|
String zipPath = PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + zipFileName;
|
|
if (ZipUtil.zip(successFileDir.getAbsolutePath(), zipPath) && new File(zipPath).exists()) {
|
|
if (ZipUtil.zip(successFileDir.getAbsolutePath(), zipPath) && new File(zipPath).exists()) {
|
|
- upload(sftpConfig.getSftp(), channelElectricField.getSftpBackupUrl(), zipPath, zipFileName);
|
|
|
|
|
|
+ SftpUtil.upload(sftpTool.getSftp(), channelElectricField.getSftpBackupUrl(), zipPath, zipFileName,true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- sftpConfig.close();
|
|
|
|
|
|
+ sftpTool.close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
log.info("-----------------结束压缩前一日解析文件并上传至sftp服务器解析任务----------------------");
|
|
log.info("-----------------结束压缩前一日解析文件并上传至sftp服务器解析任务----------------------");
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * sftp 上传文件
|
|
|
|
- *
|
|
|
|
- * @param sftp
|
|
|
|
- * @param destPathName 目标路径
|
|
|
|
- * @param srcName 源路径
|
|
|
|
- */
|
|
|
|
- private void upload(
|
|
|
|
- Sftp sftp,
|
|
|
|
- String destPathName,
|
|
|
|
- String srcName,
|
|
|
|
- String fileName
|
|
|
|
- ) {
|
|
|
|
- try (FileInputStream fis = new FileInputStream(srcName)) {
|
|
|
|
- File file = new File(srcName);
|
|
|
|
- sftp.getClient().put(fis, destPathName + "/" + fileName);
|
|
|
|
- fis.close();
|
|
|
|
- Files.deleteIfExists(file.toPath());
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("上传备份文件异常", e);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * 一个临时的sftp控制类
|
|
|
|
- */
|
|
|
|
- @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();
|
|
|
|
- channel.quit();
|
|
|
|
- channel.disconnect();
|
|
|
|
- sshSession.disconnect();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
}
|
|
}
|