public class SftpFileDownloader {
private List<String> localDestinationDirectories;
private String remoteSourceDirectory;
private String username;
private String hostname;
private int port;
private String password;
private long downloadIntervalMinutes;
public SftpFileDownloader(List<String> localDestinationDirectories, String remoteSourceDirectory,
String username, String hostname, int port, String password,
long downloadIntervalMinutes) {
this.localDestinationDirectories = localDestinationDirectories;
this.remoteSourceDirectory = remoteSourceDirectory;
this.username = username;
this.hostname = hostname;
this.port = port;
this.password = password;
this.downloadIntervalMinutes = downloadIntervalMinutes;
}
public void startPeriodicDownload() {
Runnable downloadTask = this::downloadFiles;
// 首次运行下载文件,方便手动测试
downloadTask.run();
// 设置定时任务,每隔downloadIntervalMinutes分钟执行一次下载操作
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(downloadTask, downloadIntervalMinutes, downloadIntervalMinutes, TimeUnit.MINUTES);
}
private void downloadFiles() {
JSch jsch = new JSch();
Session session = null;
ChannelSftp channelSftp = null;
try {
session = jsch.getSession(username, hostname, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
for (String localDestinationDirectory : localDestinationDirectories) {
File localDirectory = new File(localDestinationDirectory);
downloadDirectory(channelSftp, remoteSourceDirectory, localDirectory);
}
} catch (JSchException | SftpException e) {
System.err.println("Error downloading files: " + e.getMessage());
} finally {
if (channelSftp != null && channelSftp.isConnected()) {
channelSftp.disconnect();
}
if (session != null && session.isConnected()) {
session.disconnect();
}
}
}
private void downloadDirectory(ChannelSftp channelSftp, String remoteDirectory, File localDirectory) throws SftpException {
channelSftp.cd(remoteDirectory);
Vector<ChannelSftp.LsEntry> files = channelSftp.ls("*");
if (files != null) {
for (ChannelSftp.LsEntry entry : files) {
if (!entry.getAttrs().isDir()) {
downloadSingleFile(channelSftp, entry.getFilename(), localDirectory);
}
}
}
}
private void downloadSingleFile(ChannelSftp channelSftp, String remoteFileName, File localDirectory) throws SftpException {
String remoteFilePath = remoteSourceDirectory + "/" + remoteFileName;
String localFilePath = localDirectory.getAbsolutePath() + "/" + remoteFileName;
channelSftp.get(remoteFilePath, localFilePath);
System.out.println("File '" + remoteFileName + "' downloaded successfully.");
}
}
public static void main(String[] args) {
List<String> localDestinationDirectories = new ArrayList<>();
localDestinationDirectories.add("E:\\z\\file");
localDestinationDirectories.add("C:\\z\\file2");
String remoteSourceDirectory = "";
String username = "";
String hostname = "";
int port = 22;
String password = "";
long downloadIntervalMinutes = 1; // 每5分钟抓取一次
SftpFileDownloader fileDownloader = new SftpFileDownloader(localDestinationDirectories, remoteSourceDirectory,
username, hostname, port, password, downloadIntervalMinutes);
fileDownloader.startPeriodicDownload();
}