|
@@ -22,14 +22,13 @@ 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.*;
|
|
|
import java.lang.reflect.Field;
|
|
|
+import java.nio.file.Files;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
-import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
/**
|
|
|
* ftp文件解析主类
|
|
@@ -122,7 +121,6 @@ public class SftpFileParsing {
|
|
|
ParsingInterface parsingInterface = parsingInterfaceMap.get(parsingType.getFileType().name() + "Parsing");
|
|
|
parsingInterface.activationParsingConf();//初始化解析配置
|
|
|
for (ElectricField electricField : channelElectricFields) {
|
|
|
-
|
|
|
String ftpUrl = electricField.getSftpUrl();
|
|
|
List<String> fileNames = sftp.ls(ftpUrl).stream().filter(f -> f.contains(parsingType.getFileName())).collect(Collectors.toList());
|
|
|
if (fileNames.size() > 0) {
|
|
@@ -144,13 +142,12 @@ 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.separator + electricField.getStationCode());
|
|
|
+ File failFileDir = new File(PARSING_FILE_FAIL_DIR.getPath() + File.separator + DateFormatUtils.format(now, "yyyy-MM-dd") + File.separator + electricField.getStationCode() + File.separator + parsingType.getFileType().name());
|
|
|
File failFile = new File(failFileDir.getPath() + File.separator + fileName);
|
|
|
if (failFile.exists()) {
|
|
|
failFile.delete();
|
|
|
log.error("已有过解析失败文件,错误文件将覆盖!场站编号:{},文件名称:{}", electricField.getStationCode(), fileName);
|
|
|
}
|
|
|
-
|
|
|
ParsingFileUtil.move(file.getAbsolutePath(), failFileDir.getAbsolutePath(), true);
|
|
|
} catch (IOException e) {
|
|
|
log.error("解析文件失败后文件移动失败!场站编号:{},文件名称:{}", electricField.getStationCode(), fileName, e);
|
|
@@ -158,21 +155,16 @@ public class SftpFileParsing {
|
|
|
}
|
|
|
file.delete();//失败删除本地文件,等待下次下载
|
|
|
parsingLog.setParsingFileStatus("失败");
|
|
|
-
|
|
|
-
|
|
|
LogUtil.info(DataSourcesEnum.E3, AlarmEnum.E4, LogUtil.format("无法解析场站端文件:{}", fileName), electricField.getStationCode());
|
|
|
-
|
|
|
} else {
|
|
|
sftp.delFile(ftpUrl + "/" + fileName);//成功删除ftp上的文件
|
|
|
-
|
|
|
try {
|
|
|
- File successFileDir = new File(PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + DateFormatUtils.format(now, "yyyy-MM-dd") + File.separator + electricField.getStationCode());
|
|
|
+ File successFileDir = new File(PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + DateFormatUtils.format(now, "yyyy-MM-dd") + File.separator + electricField.getStationCode() + File.separator + parsingType.getFileType().name());
|
|
|
File successFile = new File(successFileDir.getPath() + File.separator + fileName);
|
|
|
if (successFile.exists()) {
|
|
|
successFile.delete();
|
|
|
log.error("已有过解析成功文件,成功文件将覆盖!场站编号:{},文件名称:{}", electricField.getStationCode(), fileName);
|
|
|
}
|
|
|
-
|
|
|
ParsingFileUtil.move(file.getAbsolutePath(), successFileDir.getAbsolutePath(), true);
|
|
|
} catch (IOException e) {
|
|
|
log.error("解析文件成功后文件移动失败!场站编号:{},文件名称:{}", electricField.getStationCode(), fileName, e);
|
|
@@ -194,7 +186,6 @@ public class SftpFileParsing {
|
|
|
// });
|
|
|
|
|
|
}
|
|
|
-
|
|
|
parsingLogService.saveBatch(parsingLogs);
|
|
|
}
|
|
|
|
|
@@ -249,26 +240,27 @@ public class SftpFileParsing {
|
|
|
|
|
|
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) {
|
|
|
+ String failSrc = PARSING_FILE_FAIL_DIR.getPath() + File.separator + dirYesToday;
|
|
|
+ String successSrc = PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + dirYesToday;
|
|
|
for (ElectricField channelElectricField : channelElectricFields) {
|
|
|
- File failFileDir = new File(PARSING_FILE_FAIL_DIR.getPath() + File.separator + dirYesToday + File.separator + channelElectricField.getStationCode());
|
|
|
+ File failFileDir = new File(failSrc + File.separator + channelElectricField.getStationCode());
|
|
|
if (failFileDir.exists()) {
|
|
|
- String zipPath = ZipUtil.zip(failFileDir.getAbsolutePath(), PARSING_FILE_FAIL_DIR.getPath() + File.separator + dirYesToday + "_FAIL.zip");
|
|
|
- if (zipPath != null && new File(zipPath).exists()) {
|
|
|
- upload(sftpConfig.getSftp(), channelElectricField.getSftpBackupUrl(), zipPath);
|
|
|
+ String zipFileName = dirYesToday + "_" + channelElectricField.getStationCode() + "_FAIL.zip";
|
|
|
+ String zipPath = PARSING_FILE_FAIL_DIR.getPath() + File.separator + zipFileName;
|
|
|
+ if (ZipUtil.zip(failFileDir.getAbsolutePath(), zipPath) && new File(zipPath).exists()) {
|
|
|
+ upload(sftpConfig.getSftp(), channelElectricField.getSftpBackupUrl(), zipPath, zipFileName);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- File successFileDir = new File(PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + dirYesToday + File.separator + channelElectricField.getStationCode());
|
|
|
+ File successFileDir = new File(successSrc + 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);
|
|
|
+ String zipFileName = dirYesToday + "_" + channelElectricField.getStationCode() + "_SUCCESS.zip";
|
|
|
+ String zipPath = PARSING_FILE_SUCCESS_DIR.getPath() + File.separator + zipFileName;
|
|
|
+ if (ZipUtil.zip(successFileDir.getAbsolutePath(), zipPath) && new File(zipPath).exists()) {
|
|
|
+ upload(sftpConfig.getSftp(), channelElectricField.getSftpBackupUrl(), zipPath, zipFileName);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -289,27 +281,16 @@ public class SftpFileParsing {
|
|
|
private void upload(
|
|
|
Sftp sftp,
|
|
|
String destPathName,
|
|
|
- String srcName
|
|
|
+ String srcName,
|
|
|
+ String fileName
|
|
|
) {
|
|
|
- FileInputStream fis = null;
|
|
|
- try {
|
|
|
- fis = new FileInputStream(srcName);
|
|
|
+ try (FileInputStream fis = new FileInputStream(srcName)) {
|
|
|
File file = new File(srcName);
|
|
|
- sftp.getClient().put(fis, destPathName + "/" + file.getName());
|
|
|
- //有没删掉文件的情况,怀疑是文件没有传输完成导致的文件被占用,静止一秒后再删除试试
|
|
|
- Thread.sleep(1000L);
|
|
|
- file.delete();
|
|
|
+ sftp.getClient().put(fis, destPathName + "/" + fileName);
|
|
|
+ fis.close();
|
|
|
+ Files.deleteIfExists(file.toPath());
|
|
|
} catch (Exception e) {
|
|
|
log.error("上传备份文件异常", e);
|
|
|
-
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- if (fis != null) {
|
|
|
- fis.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("关闭文件流异常", e);
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -334,6 +315,5 @@ public class SftpFileParsing {
|
|
|
sshSession.disconnect();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|