SFTP工具类-远程读取Linux服务器目录下所有文件

1. 需求

实现递归读取远端linux目录下所有 .sql文件

2. 远端Linux环境准备

/opt/a1目录下 创建一些 .sql的文件以及目录
在这里插入图片描述

3. 工具类封装

添加jcraft依赖:

        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.55</version>
        </dependency>

3.1 获取远程服务器SSH会话

    /**
     * 获取远程服务器SSH会话.
     * create by pushkin on 20220822.
     * TODO: 魔法值自行进行常量化处理
     *
     * @param userName userName
     * @param host     host
     * @param password password
     * @return session
     */
    private static Session connect(String userName, String host, String password) {
        JSch jsch = new JSch();
        Session session;
        try {
            session = jsch.getSession(userName, host, 22);
            log.info("获取session成功...");
        } catch (Exception e) {
            log.error("ip:{} session create error:{}", host, e.getMessage(), e);
            throw new CommonException("remote session create error:{}", e.getMessage());
        }
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        try {
            session.connect(100000);
            log.info("session 已连接...");
        } catch (Exception e) {
            log.error("ip:{} session connect time out:{}", host, e.getMessage(), e);
            throw new CommonException("session connect time out");
        }
        log.info("connect ip:{} and get session success", host);
        return session;
    }

3.2 遍历指定目录,并得到这个路径的集合list.

注意:递归处理,可以控制下遍历深度,防止栈溢出。

    /**
     * 遍历指定目录,并得到这个路径的集合list.
     * create by pushkin on 20220822.
     *
     * @param directory 路径
     * @param sftp      sftp
     * @throws SftpException SftpException
     */
    public static void getSftpPathList(String directory, ChannelSftp sftp, List<String> result) throws SftpException {
        Vector files = sftp.ls(directory);
        if (files.isEmpty()) {
            log.warn("input directory:{} is empty", directory);
            return;
        }
        for (Object object : files) {
            ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) object;
            String filename = entry.getFilename();
            if (".".equals(filename) || "..".equals(filename)) {
                continue;
            }
            if (filename.contains(".sql") || filename.contains(".SQL")) {
                log.info("add sql script file: {}", directory + "/" + filename);
                result.add( directory + "/" + filename);
            }
            SftpATTRS attrs = entry.getAttrs();
            if (attrs.isDir()) {
                String subDirectory = directory + "/" + filename;
                log.info("path: {}", subDirectory);
                getSftpPathList(subDirectory, sftp, result);
            }
        }
    }

3.3 收集指定目录下所有的文件

    /**
     * 收集指定目录下所有的文件
     * create by pushkin on 20220822.
     *
     * @param directory 指定的目录
     * @param userName  服务器登录用户名
     * @param host      服务器主机名
     * @param password  服务器登录密码
     * @return 文件集合
     */
    public static List<String> readAllFiles(String directory, String userName, String host, String password) {
        Session session = null;
        Channel channel = null;
        try {
            session = connect(userName, host, password);
            channel = session.openChannel("sftp");
            channel.connect();
            log.info("channel connect...");
            ChannelSftp nChannelSftp = (ChannelSftp) channel;
            ArrayList<String> result = new ArrayList<>();
            getSftpPathList(directory, nChannelSftp, result);
            return result;
        } catch (Exception e) {
            log.error("collectFilesOfDirectory error: {}", e.getMessage(), e);
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }
        }
        return Collections.emptyList();
    }

4. 测试

2022-08-22 15:18:21.727  INFO 13624 --- [nio-9085-exec-1] com.hathor.common.util.FileUtils         : 获取session成功...
2022-08-22 15:18:59.667  INFO 13624 --- [nio-9085-exec-1] com.hathor.common.util.FileUtils         : session 已连接...
2022-08-22 15:18:59.668  INFO 13624 --- [nio-9085-exec-1] com.hathor.common.util.FileUtils         : connect ip:192.168.60.111 and get session success
2022-08-22 15:19:46.225  INFO 13624 --- [nio-9085-exec-1] com.hathor.common.util.FileUtils         : channel connect...
2022-08-22 15:19:46.233  INFO 13624 --- [nio-9085-exec-1] com.hathor.common.util.FileUtils         : add sql script file: /opt/a1/demo1.sql
2022-08-22 15:19:46.233  INFO 13624 --- [nio-9085-exec-1] com.hathor.common.util.FileUtils         : add sql script file: /opt/a1/demo2.SQL
2022-08-22 15:19:46.233  INFO 13624 --- [nio-9085-exec-1] com.hathor.common.util.FileUtils         : path: /opt/a1/b1
2022-08-22 15:19:46.235  INFO 13624 --- [nio-9085-exec-1] com.hathor.common.util.FileUtils         : add sql script file: /opt/a1/b1/demo3.sql
2022-08-22 15:19:46.236  INFO 13624 --- [nio-9085-exec-1] com.hathor.common.util.FileUtils         : path: /opt/a1/b1/c1
2022-08-22 15:19:46.238  INFO 13624 --- [nio-9085-exec-1] com.hathor.common.util.FileUtils         : path: /opt/a1/b1/c2
2022-08-22 15:19:46.240  INFO 13624 --- [nio-9085-exec-1] com.hathor.common.util.FileUtils         : add sql script file: /opt/a1/b1/c2/demo6.sql
2022-08-22 15:19:46.241  INFO 13624 --- [nio-9085-exec-1] com.hathor.common.util.FileUtils         : add sql script file: /opt/a1/b1/c2/demo7.sql
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pushkin.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值