java定时下载文件_Java使用sftp定时下载文件

@Service

public class SftpTask extends Thread {

private ChannelSftp sftp;

private Session session;

@Value("${sftp.admin.username}")

private String username;

@Value("${sftp.admin.password}")

private String password;

@Value("${sftp.host}")

private String host;

@Value("${sftp.port}")

private Integer port;

private SftpService sftpService;

public EtlSftpTask (SftpService sftpService) {

this.sftpService = sftpService;

}

/**

* 建立sftp连接

*/

private void connect(){

try {

JSch jSch = new JSch();

session = jSch.getSession(username, host, port);

session.setPassword(password);

session.setConfig("StrictHostKeyChecking", "no");

session.connect();

Channel channel = session.openChannel("sftp");

channel.connect();

sftp = (ChannelSftp) channel;

}catch (JSchException e) {

e.printStackTrace();

}

}

/**

* 关闭sftp连接

*/

public void close(){

try {

if (sftp != null) {

if (sftp.isConnected()) sftp.disconnect();

}

if(session != null){

if (session.isConnected()) session.disconnect();

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 下载文件到本地

*

* @param source 源文件

* @param target 目标文件

* @throws SftpException 异常

* @throws FileNotFoundException 异常

*/

private void download(String source, String target) throws SftpException, FileNotFoundException {

sftp.get(source, new FileOutputStream(new File(target)));

}

/**

* 处理用户数据文件

*

* @param root 数据文件根目录

* @param lastTime 上次处理文件的最后的时间

* @return 本次处理文件的最后的时间

*/

private Integer handle(String root, Integer lastTime) {

String directory = root + "/event/";

Vector files;

try {

files = sftp.ls(directory + "event_*.csv");

} catch (Exception e) {

e.printStackTrace();

return 0;

}

// 文件名

String fileName;

// 临时文件

String tmpFile;

// 文件更新时间

Integer mTime;

// 文件最后更新时间

Integer maxTime = lastTime;

// 处理用户文件

for(Object o: files) {

try {

ChannelSftp.LsEntry f = (ChannelSftp.LsEntry) o;

// 文件更新时间

mTime = f.getAttrs().getMTime();

if (mTime <= lastTime) continue;

// 文件名

fileName = f.getFilename();

// 最后处理事件

maxTime = Math.max(maxTime, mTime);

// 下载文件

tmpFile = "/tmp/" + fileName;

download(directory + fileName, tmpFile);

} catch (Exception e) {

// TODO 错误日志

e.printStackTrace();

}

}

// 返回文件最后的处理时间

return maxTime;

}

/**

* 每天凌晨1点开始执行

*/

@Scheduled(cron = "0 0 1 * * *")

public void task () {

// 获取sftp连接

connect();

String root;

Integer lastTime;

Long cid;

Integer maxTime = lastTime;

// 获取用户列表

for (SftpDTO sftpDTO: sftpService.findAll()) {

// 用户主目录

root = sftpDTO.getSftpRoot();

// 上次处理文件的最后时间

lastTime = sftpDTO.getLastTime();

maxTime = Math.max(maxTime, handle(root, lastTime));

// 更新最后处理时间

if (!maxTime.equals(lastTime)) {

sftpDTO.setLastTime(maxTime);

sftpService.update(sftpDTO);

}

}

// 释放sftp资源

close();

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值