|
@@ -13,21 +13,23 @@ 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.ZipUtil;
|
|
import com.jcraft.jsch.ChannelSftp;
|
|
import com.jcraft.jsch.ChannelSftp;
|
|
import com.jcraft.jsch.JSch;
|
|
import com.jcraft.jsch.JSch;
|
|
import com.jcraft.jsch.Session;
|
|
import com.jcraft.jsch.Session;
|
|
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.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
|
+import java.io.*;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Field;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
/**
|
|
/**
|
|
* ftp文件解析
|
|
* 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_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;
|
|
|
|
|
|
@@ -71,26 +74,29 @@ public class SftpFileParsing {
|
|
*/
|
|
*/
|
|
public void parsingFile() {
|
|
public void parsingFile() {
|
|
|
|
|
|
-
|
|
|
|
log.info("-----------------开始执行FTP文件解析任务----------------------");
|
|
log.info("-----------------开始执行FTP文件解析任务----------------------");
|
|
|
|
|
|
List<ParsingType> parsingTypes = parsingTypeService.list();
|
|
List<ParsingType> parsingTypes = parsingTypeService.list();
|
|
|
|
|
|
if (parsingTypes.size() > 0) {
|
|
if (parsingTypes.size() > 0) {
|
|
|
|
|
|
- List<SftpChannel> channels = sftpChannelService.list();
|
|
|
|
|
|
+ List<SftpChannel> sftpChannels = sftpChannelService.list();
|
|
|
|
|
|
List<ElectricField> electricFields = electricFieldService.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) {
|
|
if (channelElectricFields.size() > 0) {
|
|
- executeSftpParsing(channel, parsingTypes, electricFields);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ SftpConfig sftpConfig = createSftp(sftpChannel);
|
|
|
|
+ if (sftpConfig.getSftp() != null) {
|
|
|
|
+ parsingFile(parsingTypes, channelElectricFields, sftpConfig.getSftp());
|
|
|
|
+ sftpConfig.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
log.info("未配置文件识别类型标识,无法进行下载解析");
|
|
log.info("未配置文件识别类型标识,无法进行下载解析");
|
|
}
|
|
}
|
|
@@ -136,7 +142,7 @@ public class SftpFileParsing {
|
|
parsingLog.setStationCode(electricField.getStationCode());
|
|
parsingLog.setStationCode(electricField.getStationCode());
|
|
if (parsingResultDto.getStatus().equals("fail")) {
|
|
if (parsingResultDto.getStatus().equals("fail")) {
|
|
try {
|
|
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);
|
|
File failFile = new File(failFileDir.getPath() + File.separator + fileName);
|
|
if (failFile.exists()) {
|
|
if (failFile.exists()) {
|
|
failFile.delete();
|
|
failFile.delete();
|
|
@@ -158,7 +164,7 @@ public class SftpFileParsing {
|
|
sftp.delFile(ftpUrl + "/" + fileName);//成功删除ftp上的文件
|
|
sftp.delFile(ftpUrl + "/" + fileName);//成功删除ftp上的文件
|
|
|
|
|
|
try {
|
|
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);
|
|
File successFile = new File(successFileDir.getPath() + File.separator + fileName);
|
|
if (successFile.exists()) {
|
|
if (successFile.exists()) {
|
|
successFile.delete();
|
|
successFile.delete();
|
|
@@ -195,41 +201,118 @@ public class SftpFileParsing {
|
|
* 创建sftp 并解析
|
|
* 创建sftp 并解析
|
|
*
|
|
*
|
|
* @param sftpChannel
|
|
* @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 sftp = null;
|
|
|
|
- JSch jsch = new JSch();
|
|
|
|
- ChannelSftp channel = null;
|
|
|
|
- Session sshSession = null;
|
|
|
|
|
|
+ SftpConfig sftpConfig = new SftpConfig();
|
|
|
|
+
|
|
try {
|
|
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 {
|
|
} 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客户端关闭");
|
|
log.info("sftp客户端关闭");
|
|
JschUtil.closeAll();
|
|
JschUtil.closeAll();
|
|
sftp.close();
|
|
sftp.close();
|
|
@@ -238,6 +321,6 @@ public class SftpFileParsing {
|
|
sshSession.disconnect();
|
|
sshSession.disconnect();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|