|
@@ -0,0 +1,136 @@
|
|
|
|
+package com.jiayue.ipp.idp.service.an;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import cn.hutool.extra.ssh.Sftp;
|
|
|
|
+import com.jiayue.ipp.common.data.entity.an.ParsingChannel;
|
|
|
|
+import com.jiayue.ipp.common.data.entity.an.ParsingLog;
|
|
|
|
+import com.jiayue.ipp.common.data.entity.an.ParsingUrl;
|
|
|
|
+import com.jiayue.ipp.idp.dto.FileAnalysisStatusDto;
|
|
|
|
+import com.jiayue.ipp.idp.util.FileUtil;
|
|
|
|
+import com.jiayue.ipp.idp.util.sftp.SftpTool;
|
|
|
|
+import com.jiayue.ipp.idp.util.sftp.SftpUtil;
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
+import org.apache.commons.lang.time.DateFormatUtils;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+import org.springframework.util.ResourceUtils;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileNotFoundException;
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
+import java.net.URLDecoder;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * ftp通信业务层
|
|
|
|
+ *
|
|
|
|
+ * @author tl
|
|
|
|
+ * @date 2022-05-11 09:51:21
|
|
|
|
+ */
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
+@Slf4j
|
|
|
|
+@Service
|
|
|
|
+@Transactional
|
|
|
|
+public class SFTPService {
|
|
|
|
+
|
|
|
|
+ private final ParsingUrlService parsingUrlService;
|
|
|
|
+
|
|
|
|
+ private final ParsingFileService parsingFileService;
|
|
|
|
+
|
|
|
|
+ private final ParsingLogService parsingLogService;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 逻辑过程:
|
|
|
|
+ * 获取ftp解析的通道进行ftp连接
|
|
|
|
+ * 通过ftp的下载路径下载对应场站和预测厂家目录中的文件到本地目录
|
|
|
|
+ * 进行文件解析
|
|
|
|
+ * 解析成功后,文件上传到minio
|
|
|
|
+ * 将记录存入数据库
|
|
|
|
+ * 备份并删除本地文件(备份文件应定时清理)
|
|
|
|
+ * 解析成功并结束
|
|
|
|
+ */
|
|
|
|
+ public void sftp(ParsingChannel sftpParsingChannel) {
|
|
|
|
+
|
|
|
|
+ List<ParsingUrl> ftpParsingUrls = parsingUrlService.list();
|
|
|
|
+ //ftp连接
|
|
|
|
+ SftpTool sftpTool = SftpUtil.createSftp(sftpParsingChannel);
|
|
|
|
+
|
|
|
|
+ //判断是否连接成功,成功后业务继续
|
|
|
|
+ if (sftpTool.getSftp() != null) {
|
|
|
|
+ //过滤出当前解析通道下在使用的《 解析路径 》
|
|
|
|
+ List<ParsingUrl> ftpParsingUrlList = ftpParsingUrls.stream().filter(s -> s.getCId().equals(sftpParsingChannel.getId()) && s.getUrlStatus().equals("1")).collect(Collectors.toList());
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Sftp sftp = sftpTool.getSftp();
|
|
|
|
+ String dateDir = DateFormatUtils.format(new Date(), "yyyyMMdd");
|
|
|
|
+ //遍历解析路径,对文件进行解析
|
|
|
|
+ for (ParsingUrl ftpParsingUrl : ftpParsingUrlList) {
|
|
|
|
+ String url = ftpParsingUrl.getUrl();
|
|
|
|
+ List<String> strings = sftp.ls(url);
|
|
|
|
+ //遍历文件
|
|
|
|
+ int i = 0;
|
|
|
|
+ for (String fileName : strings) {
|
|
|
|
+ if (i > 20) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ i++;
|
|
|
|
+ try {
|
|
|
|
+ ParsingLog parsingLog = new ParsingLog();
|
|
|
|
+ parsingLog.setFileName(fileName);
|
|
|
|
+ parsingLog.setStationCode(ftpParsingUrl.getStationCode());
|
|
|
|
+ parsingLog.setParsingTime(new Date());
|
|
|
|
+ // 从ftp下载到本地parsing目录里
|
|
|
|
+ String path = FileUtil.getParsingPath() + File.separator + ftpParsingUrl.getStationCode() + File.separator + ftpParsingUrl.getForecastManufactor() + File.separator + dateDir;
|
|
|
|
+ File dirFile = checkGetPath(path);
|
|
|
|
+ //下载文件到临时目录
|
|
|
|
+ sftp.get(url + "/" + fileName, dirFile.getPath() + File.separator + fileName);
|
|
|
|
+ File file = FileUtils.getFile(dirFile, fileName);
|
|
|
|
+ if (file.length() <= 0) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //定义解析的类型,默认为错误(未知),成功后为文件类型,也会作为存储目录名
|
|
|
|
+ FileAnalysisStatusDto fileAnalysisStatusDto = parsingFileService.parsingFile(file, ftpParsingUrl);
|
|
|
|
+ parsingLog.setFileType(fileAnalysisStatusDto.getFileType());
|
|
|
|
+ if (!"1".equals(fileAnalysisStatusDto.getStatus())) {
|
|
|
|
+ // 解析失败
|
|
|
|
+ file.delete();
|
|
|
|
+ parsingLog.setParsingFileStatus("0");
|
|
|
|
+ parsingLog.setParsingDescribe(fileAnalysisStatusDto.getMessage());
|
|
|
|
+ } else {
|
|
|
|
+ // 解析成功,删除ftp上的文件
|
|
|
|
+ sftp.delFile(url + "/" + fileName);
|
|
|
|
+ parsingLog.setParsingFileStatus("1");
|
|
|
|
+ parsingLog.setParsingDescribe("文件解析成功");
|
|
|
|
+ }
|
|
|
|
+ parsingLogService.save(parsingLog);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(ftpParsingUrl.getStationCode() + "文件解析失败" + fileName, e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info("{} sftp下载文件并解析执行完成", sftpParsingChannel.getChannelName());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("{} sftp下载文件并解析过程失败", sftpParsingChannel.getChannelName(), e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ sftpTool.close();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static File checkGetPath(String path){
|
|
|
|
+ File file = new File(path);
|
|
|
|
+ if (!file.exists()){
|
|
|
|
+ file.mkdirs();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return file;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|