sftp 定时下载一个目录下所有文件并删除原目录下文件 java 实现
时间: 2024-02-06 14:04:35 浏览: 163
你可以使用 JSch 库实现 SFTP 客户端,并使用定时任务框架(如 Quartz)实现定时任务。具体实现步骤如下:
1. 引入 JSch 库,可以通过 Maven 或 Gradle 进行引入,或者手动下载并导入。
2. 使用 JSch 创建 SFTP 客户端连接,连接代码如下:
```java
JSch jsch = new JSch();
Session session = jsch.getSession(username, host, port);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
```
3. 使用 `channel.ls()` 方法获取指定目录下的所有文件,并使用 `channel.get()` 方法下载文件,下载代码如下:
```java
List<ChannelSftp.LsEntry> fileList = channel.ls(remotePath);
for (ChannelSftp.LsEntry file : fileList) {
if (!file.getAttrs().isDir()) {
String remoteFile = remotePath + "/" + file.getFilename();
String localFile = localPath + "/" + file.getFilename();
channel.get(remoteFile, localFile);
}
}
```
4. 将下载完成的文件从 SFTP 服务器上删除,删除代码如下:
```java
for (ChannelSftp.LsEntry file : fileList) {
if (!file.getAttrs().isDir()) {
String remoteFile = remotePath + "/" + file.getFilename();
channel.rm(remoteFile);
}
}
```
5. 将以上代码封装成一个方法,并使用定时任务框架定时调用即可。完整代码如下:
```java
public class SftpDownloadTask {
private String host;
private int port;
private String username;
private String password;
private String remotePath;
private String localPath;
public void run() throws JSchException, SftpException {
JSch jsch = new JSch();
Session session = jsch.getSession(username, host, port);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
List<ChannelSftp.LsEntry> fileList = channel.ls(remotePath);
for (ChannelSftp.LsEntry file : fileList) {
if (!file.getAttrs().isDir()) {
String remoteFile = remotePath + "/" + file.getFilename();
String localFile = localPath + "/" + file.getFilename();
channel.get(remoteFile, localFile);
}
}
for (ChannelSftp.LsEntry file : fileList) {
if (!file.getAttrs().isDir()) {
String remoteFile = remotePath + "/" + file.getFilename();
channel.rm(remoteFile);
}
}
channel.disconnect();
session.disconnect();
}
public static void main(String[] args) throws SchedulerException {
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(SftpDownloadJob.class)
.withIdentity("SftpDownloadJob", "SftpDownloadGroup")
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("SftpDownloadTrigger", "SftpDownloadGroup")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ?")) // 每天 0 点运行
.build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
}
public static class SftpDownloadJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
SftpDownloadTask task = new SftpDownloadTask();
task.host = "sftp.example.com";
task.port = 22;
task.username = "username";
task.password = "password";
task.remotePath = "/remote/path";
task.localPath = "/local/path";
try {
task.run();
} catch (JSchException | SftpException e) {
throw new JobExecutionException(e);
}
}
}
}
```
阅读全文