feat: 增加定时任务下载文件保存本地
parent
7fc8867336
commit
1c7839afa6
@ -0,0 +1,119 @@
|
|||||||
|
package com.xydl.cac.task;
|
||||||
|
|
||||||
|
import com.jcraft.jsch.ChannelSftp;
|
||||||
|
import com.xydl.cac.entity.*;
|
||||||
|
import com.xydl.cac.service.RemoteConfigService;
|
||||||
|
import com.xydl.cac.service.RemoteDownloadService;
|
||||||
|
import com.xydl.cac.socket.WebSocketServer;
|
||||||
|
import com.xydl.cac.util.DingTalkPushUtil;
|
||||||
|
import com.xydl.cac.util.SFTPTool;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class SftpDownloadTask {
|
||||||
|
|
||||||
|
@Value("${cac.data-path}")
|
||||||
|
public String dataPath;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
RemoteConfigService configService;
|
||||||
|
@Resource
|
||||||
|
RemoteDownloadService downloadService;
|
||||||
|
@Resource
|
||||||
|
DingTalkPushUtil dingTalkPushUtil;
|
||||||
|
@Resource
|
||||||
|
WebSocketServer webSocketServer;
|
||||||
|
|
||||||
|
@Scheduled(cron = "0 3 * * * ?")
|
||||||
|
public void downloadAll() {
|
||||||
|
List<RemoteConfig> configList = configService.listAll();
|
||||||
|
if (!CollectionUtils.isEmpty(configList)) {
|
||||||
|
log.info("SftpDownloadTask.downloadAll 开始.");
|
||||||
|
try {
|
||||||
|
for (RemoteConfig config : configList) {
|
||||||
|
this.downloadServer(config);
|
||||||
|
}
|
||||||
|
log.info("SftpDownloadTask.downloadAll 结束.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("SftpDownloadTask.downloadAll error.", e);
|
||||||
|
String str = "SFTP下载文件异常: " + e.getMessage();
|
||||||
|
webSocketServer.sendMessage(str);
|
||||||
|
dingTalkPushUtil.pushText(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 下载一个远端服务器
|
||||||
|
private void downloadServer(RemoteConfig config) throws Exception {
|
||||||
|
if (config.getActive() == null || config.getActive().intValue() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
config.toList();
|
||||||
|
if (config.getPathList() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SFTPTool sftpTool = new SFTPTool();
|
||||||
|
try {
|
||||||
|
sftpTool.connect(config.getIp(), config.getPort(), config.getUser(), config.getPasswd());
|
||||||
|
|
||||||
|
for (String remotePath : config.getPathList()) {
|
||||||
|
this.downloadPath(remotePath, config, sftpTool);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("SftpDownloadTask.downloadServer error.", e);
|
||||||
|
String str = "SFTP下载文件异常: " + e.getMessage();
|
||||||
|
webSocketServer.sendMessage(str);
|
||||||
|
dingTalkPushUtil.pushText(str);
|
||||||
|
} finally {
|
||||||
|
sftpTool.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void downloadPath(String remotePath, RemoteConfig config, SFTPTool sftpTool) throws Exception {
|
||||||
|
String str = "abcd" + remotePath.replaceAll("/", "").replaceAll("\\\\", "");
|
||||||
|
str = str.substring(str.length() - 4);
|
||||||
|
String localPath = "/ampli/" + config.getIp() + "/" + str;
|
||||||
|
File dir = new File(dataPath + localPath);
|
||||||
|
dir.mkdirs();
|
||||||
|
|
||||||
|
Vector<ChannelSftp.LsEntry> files = sftpTool.listFiles(remotePath);
|
||||||
|
for (ChannelSftp.LsEntry file : files) {
|
||||||
|
this.downloadFile(remotePath, file.getFilename(), localPath, config, sftpTool);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void downloadFile(String remotePath, String filename,
|
||||||
|
String localPath, RemoteConfig config, SFTPTool sftpTool) throws Exception {
|
||||||
|
if (StringUtils.isBlank(config.getSuffix()) || filename.toLowerCase().endsWith(config.getSuffix().toLowerCase())) {
|
||||||
|
RemoteDownload item = new RemoteDownload();
|
||||||
|
item.setConfigId(config.getId());
|
||||||
|
item.setRemotePath(remotePath);
|
||||||
|
item.setFilename(filename);
|
||||||
|
|
||||||
|
boolean exist = downloadService.exist(item);
|
||||||
|
if (!exist) {
|
||||||
|
String localFilePath = localPath + "/" + filename;
|
||||||
|
sftpTool.download(filename, dataPath + localFilePath);
|
||||||
|
if (config.getTodel() != null && config.getTodel().intValue() == 1) {
|
||||||
|
sftpTool.delete(filename);
|
||||||
|
}
|
||||||
|
item.setPath("/data" + localFilePath);
|
||||||
|
item.setCreateTime(new Date());
|
||||||
|
downloadService.add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue