Java使用sftp定时下载文件的示例代码
作者:tianshl 时间:2022-11-14 06:11:47
sftp简介
sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的网络的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。SFTP 为 SSH的其中一部分,是一种传输档案至 Blogger 伺服器的安全方式。其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol)的安全文件信息传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接和答复操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。
添加依赖
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
增加配置
sftp:
ip: 192.168.1.60
port: 22
timeout: 60000
retryTime: 3
admin:
username: admin
password: 2k3xrYjbd930.
代码示例
每天凌晨1点在多个用户目录中下载csv文件至本地tmp目录
@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();
}
}
总结
以上所述是小编给大家介绍的Java使用sftp定时下载文件的示例代码网站的支持!
来源:https://my.oschina.net/tianshl/blog/1811017
标签:java,sftp,下载,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis实现增删改查(CRUD)实例代码
2022-10-05 20:03:22
Java ArrayList深入源码层分析
2021-10-18 04:12:31
![](https://img.aspxhome.com/file/2023/9/60919_0s.png)
Hibernate一级缓存和二级缓存详解
2023-11-16 11:58:11
springboot读取文件,打成jar包后访问不到的解决
2023-07-14 13:56:35
Java针对ArrayList自定义排序的2种实现方法
2022-01-30 03:47:34
Kotlin编程基础语法编码规范
2023-06-20 16:50:07
![](https://img.aspxhome.com/file/2023/6/105846_0s.jpg)
JDK线程池和Spring线程池的使用实例解析
2023-02-24 13:27:32
flutter窗口初始和绘制流程详析
2023-08-17 21:07:30
浅谈c++性能测试工具之计算时间复杂度
2023-07-09 12:40:05
![](https://img.aspxhome.com/file/2023/4/79524_0s.jpg)
Android使用JobScheduler定期推送本地通知实例代码
2023-07-26 22:43:28
java开发之MD5加密算法的实现
2022-05-13 23:44:35
Flutter模仿实现微信底部导航栏流程详解
2023-06-21 11:46:12
![](https://img.aspxhome.com/file/2023/3/100603_0s.png)
SpringBoot详细讲解静态资源导入的实现
2023-07-26 13:23:21
![](https://img.aspxhome.com/file/2023/8/57728_0s.png)
基于jdk1.8的Java源码详解 Integer
2023-05-08 11:32:22
![](https://img.aspxhome.com/file/2023/8/68158_0s.jpg)
java生成指定范围随机数的多种代码
2023-04-28 07:51:15
![](https://img.aspxhome.com/file/2023/3/60383_0s.png)
Java如何定义Long类型
2022-05-28 22:28:52
![](https://img.aspxhome.com/file/2023/6/62246_0s.png)
java使用ffmpeg处理视频的方法
2023-07-28 22:58:08
![](https://img.aspxhome.com/file/2023/9/57719_0s.jpg)
SpringBoot集成阿里云OSS图片上传
2021-08-15 21:01:55
![](https://img.aspxhome.com/file/2023/9/66979_0s.jpg)
java实现简单银行家算法
2022-01-31 23:33:19
![](https://img.aspxhome.com/file/2023/7/63167_0s.jpg)
使用springboot aop来实现读写分离和事物配置
2022-10-29 11:43:07
![](https://img.aspxhome.com/file/2023/6/69086_0s.jpg)