首页>代码>Java连接Linux服务器并上传文件、下载文件、发送指令>/new_maven/src/main/java/com/hai/linux/sftpConnection.java
package com.hai.linux;

import com.hai.util.sftpConnectionUtil;
import com.jcraft.jsch.*;
import org.junit.Test;

//测试
public class sftpConnection {

    @Test
    public void testUpload(){
        Session sshSession = sftpConnectionUtil.connect("xxx.xxx.xxx.xx", 22, "root", "xxx");
        ChannelSftp sftp = sftpConnectionUtil.sftpConnect(sshSession);
        sftpConnectionUtil.upload("/data5","testlinux/aa1.txt",sftp);
        sftpConnectionUtil.closeResource(sshSession,sftp);
    }

    @Test
    public void testDownload(){
        Session sshSession = sftpConnectionUtil.connect("xxx.xxx.xxx.xx", 22, "root", "xxx");
        ChannelSftp sftp = sftpConnectionUtil.sftpConnect(sshSession);
        sftpConnectionUtil.download("/data5","aa1.txt","testlinux/aa1.txt",sftp);
        sftpConnectionUtil.closeResource(sshSession,sftp);
    }

    @Test
    public void testExecCmd(){
        Session sshSession = sftpConnectionUtil.connect("xxx.xxx.xxx.xx", 22, "root", "xxx");
        ChannelExec channelExec = sftpConnectionUtil.execConnect(sshSession);
        sftpConnectionUtil.execCmd("cat /data5/aa1.txt",channelExec);
        sftpConnectionUtil.closeResource(sshSession,channelExec);
    }


}
最近下载更多
438265764  LV14 2023年7月31日
15866685272  LV3 2023年5月4日
林间听风  LV10 2023年2月1日
lironggang  LV38 2022年11月20日
lxsnh123  LV3 2022年11月18日
skipple3  LV39 2022年10月18日
zw050256  LV7 2022年9月30日
wuyu8995861  LV7 2022年8月8日
liuyu-zui  LV4 2022年8月1日
CrystalQ  LV8 2022年7月9日
最近浏览更多
LARY  LV1 1月5日
tangjianzhong 2023年12月19日
暂无贡献等级
kkkxyh  LV13 2023年12月11日
jiemomo  LV12 2023年10月19日
19050126312  LV1 2023年10月19日
2036495585  LV9 2023年9月25日
438265764  LV14 2023年7月31日
zw5097  LV23 2023年7月15日
tangyongheng08  LV6 2023年6月15日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友