feat: 增加定时任务下载文件保存本地

haikang
huangfeng 9 months ago
parent 7fc8867336
commit 1c7839afa6

@ -29,8 +29,6 @@ public class RemoteConfigController extends BasicController {
RemoteConfigService configService; RemoteConfigService configService;
@Resource @Resource
RemoteDownloadService downloadService; RemoteDownloadService downloadService;
@Resource
SFTPTool sftpTool;
@GetMapping("listAll") @GetMapping("listAll")
@ApiOperation("查询全部列表") @ApiOperation("查询全部列表")
@ -70,6 +68,7 @@ public class RemoteConfigController extends BasicController {
@ApiOperation("测试") @ApiOperation("测试")
public Response<String> test(Integer id) throws Exception { public Response<String> test(Integer id) throws Exception {
RemoteConfig item = configService.get(id); RemoteConfig item = configService.get(id);
SFTPTool sftpTool = new SFTPTool();
sftpTool.connect(item.getIp(), item.getPort(), item.getUser(), item.getPasswd()); sftpTool.connect(item.getIp(), item.getPort(), item.getUser(), item.getPasswd());
sftpTool.disconnect(); sftpTool.disconnect();
return Response.success("正常"); return Response.success("正常");

@ -30,6 +30,10 @@ public class RemoteDownload {
@Column(name = "config_id") @Column(name = "config_id")
private Integer configId; private Integer configId;
@ApiModelProperty("路径")
@Column(name = "remote_path")
private String remotePath;
@ApiModelProperty("路径") @ApiModelProperty("路径")
@Column(name = "path") @Column(name = "path")
private String path; private String path;

@ -5,8 +5,11 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository @Repository
public interface RemoteDownloadRepository extends JpaRepository<RemoteDownload, Integer>, JpaSpecificationExecutor<RemoteDownload> { public interface RemoteDownloadRepository extends JpaRepository<RemoteDownload, Integer>, JpaSpecificationExecutor<RemoteDownload> {
List<RemoteDownload> findByConfigIdAndRemotePathAndFilename(Integer configId, String remotePath, String filename);
} }

@ -7,4 +7,7 @@ public interface RemoteDownloadService {
Page<RemoteDownload> list(Integer configId, int pageNum, int pageSize) throws Exception; Page<RemoteDownload> list(Integer configId, int pageNum, int pageSize) throws Exception;
void add(RemoteDownload item);
boolean exist(RemoteDownload item);
} }

@ -55,4 +55,21 @@ public class RemoteDownloadServiceImpl implements RemoteDownloadService {
} }
} }
} }
@Override
public void add(RemoteDownload item) {
item.setId(null);
repository.save(item);
}
@Override
public boolean exist(RemoteDownload item) {
List<RemoteDownload> list = repository.findByConfigIdAndRemotePathAndFilename(item.getConfigId(), item.getRemotePath(), item.getFilename());
if (CollectionUtils.isEmpty(list)) {
return false;
} else {
return true;
}
}
} }

@ -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);
}
}
}
}

@ -1,15 +1,11 @@
package com.xydl.cac.util; package com.xydl.cac.util;
import com.jcraft.jsch.*; import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.*; import java.io.*;
import java.util.Properties; import java.util.Properties;
import java.util.Vector; import java.util.Vector;
@Component
@Slf4j
public class SFTPTool { public class SFTPTool {
Session session = null; Session session = null;
ChannelSftp sftp = null; ChannelSftp sftp = null;
@ -54,19 +50,20 @@ public class SFTPTool {
} }
} }
public void upload(String localFilePath, String remoteFilePath) throws Exception { public void upload(String localFilePath, String remoteFileName) throws Exception {
sftp.put(new FileInputStream(localFilePath), remoteFilePath); sftp.put(new FileInputStream(localFilePath), remoteFileName);
} }
public void download(String remoteFilePath, String localFilePath) throws Exception { public void download(String remoteFileName, String localFilePath) throws Exception {
sftp.get(remoteFilePath, new FileOutputStream(localFilePath)); sftp.get(remoteFileName, new FileOutputStream(localFilePath));
} }
public void delete(String remoteFilePath) throws SftpException { public void delete(String remoteFileName) throws SftpException {
sftp.rm(remoteFilePath); sftp.rm(remoteFileName);
} }
public Vector listFiles(String remotePath) throws SftpException { public Vector listFiles(String remotePath) throws SftpException {
sftp.cd(remotePath);
return sftp.ls(remotePath); return sftp.ls(remotePath);
} }

@ -9,7 +9,7 @@ spring:
time-zone: GMT+8 time-zone: GMT+8
datasource: datasource:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.190:3306/cacdb_panlong?charset=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&connectTimeout=60000&socketTimeout=60000 url: jdbc:mysql://192.168.1.190:3306/cacdb_fuxian?charset=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&connectTimeout=60000&socketTimeout=60000
username: root username: root
password: 123456 password: 123456
jpa: jpa:
@ -30,6 +30,7 @@ spring:
size: 5 size: 5
cac: cac:
data-path: /home/xydl/ncac/data
i2: i2:
enable: false enable: false
url: http://192.168.1.190:8080/busi-back-ws/service/XydlService url: http://192.168.1.190:8080/busi-back-ws/service/XydlService

@ -30,6 +30,7 @@ spring:
size: 5 size: 5
cac: cac:
data-path: /home/xydl/ncac/data
i2: i2:
enable: false enable: false
url: http://192.168.1.190:8080/busi-back-ws/service/XydlService url: http://192.168.1.190:8080/busi-back-ws/service/XydlService

Loading…
Cancel
Save