将服务器文件下载到本地

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值