当前位置: 首页 > article >正文

Java利用JSch进行SFTP进行SSH连接远程服务器进行目录创建与上传文件,ChannelSftp

Maven依赖

        <dependency>
            <groupId>com.github.mwiede</groupId>
            <artifactId>jsch</artifactId>
            <version>0.2.23</version>
        </dependency>

测试代码

    public void sftpTest() {
        Session session = null;
        //打开SFTP通道
        ChannelSftp channel = null;
        try {
            JSch jsch = new JSch();
            session = jsch.getSession("root", "zym.test.com", 29620);
            session.setPassword("V6LbAzgKrD5q");//远程密码
            session.setConfig("StrictHostKeyChecking", "no"); // 禁用严格主机密钥检查
            session.connect();
            //打开SFTP通道
            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            //查看服务器当前目录内容
            Vector<ChannelSftp.LsEntry> entries = channel.ls("/root/autodl-tmp/ComfyUI/input");
            System.out.println("entries = " + entries);
            String remotePath = "/root/autodl-tmp/ComfyUI/input/video";
            //上传前目录video目录内容查看
            Vector<ChannelSftp.LsEntry> beforeDir = channel.ls(remotePath);
            System.out.println("beforeDir = " + beforeDir);
            //远程目录创建,如存在则忽略
            mkDirs(channel, remotePath);
            //将本地视频上传到远程服务
            channel.put("C:\\Users\\EDY\\Desktop\\11月21日-1.mp4", remotePath + "/11月21日-1.mp4", ChannelSftp.OVERWRITE);
            //上传后目录查看
            Vector<ChannelSftp.LsEntry> afterDir = channel.ls(remotePath);
            System.out.println("afterDir = " + afterDir);
        } catch (JSchException | SftpException e) {
            e.printStackTrace();
        } finally {
            if (channel != null) channel.disconnect();//关闭通道
            if (session != null) session.disconnect();//关闭会话
        }
    }
    /**
     * 创建目录
     *
     * @param channel 打开的SFTP通道
     * @param path    路径格式例如:/root/autodl-tmp/ComfyUI/input/video
     */
    private void mkDirs(ChannelSftp channel, String path) throws SftpException {
        String[] dirs = path.split("/");
        StringBuilder currentPath = new StringBuilder();
        for (String dir : dirs) {
            if (dir.isEmpty()) continue;  // 处理路径开头为"/"的情况
            currentPath.append("/").append(dir);
            try {
                channel.lstat(currentPath.toString());  // 检查目录是否存在
            } catch (SftpException e) {
                if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
                    channel.mkdir(currentPath.toString()); //创建不存在的层级目录
                }
            }
        }
    }

运行效果

1、/root/autodl-tmp/ComfyUI/input目录内容的查看
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/ddad7fa797f34e79b860ae5fc0619637.png
2、上传前查看目录内容
在这里插入图片描述
3、上传后查看目录内容
在这里插入图片描述


http://www.kler.cn/a/572468.html

相关文章:

  • 学单片机能从事什么工作?
  • 【华为OD机考】华为OD笔试真题解析(20)--投篮大赛
  • 安卓广播的使用
  • Elasticsearch:简化大数据分析,使用Elasticsearch进行高效数据聚合
  • LeetCode hot 100—轮转数组
  • visual studio 2022 手工写一个简单的MFC程序
  • [原创](Modern C++)现代C++的关键性概念: 什么是友元函数, 什么是友元类?
  • css不出现滚动条
  • T-SQL 语言基础: SQL 数据库对象元数据及配置信息获取
  • 计算机网络——子网掩码
  • 网络安全中keli是什么
  • 初识Qt · 实现hello world的N种细节和对象树
  • Springboot快速接入Deepseek
  • 椭圆函数3D双重周期性交互式演示工具
  • 【hot100】102二叉树的层序遍历
  • 生态安全相关
  • Linux搜索---find
  • 【后端开发面试题】每日 3 题(六)
  • 使用ffmpeg读取mp4文件解码失败
  • Android+SpringBoot的老年人健康饮食小程序平台