java链接SFTP服务器

需要使用的jar包

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

</dependency>  

工具类:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class SftpUtils {
    private static Session session = null;
    private static ChannelSftp channel = null;
    private static List<String> sftpList = new ArrayList<>();

    // 1.获取sftp连接
    public static ChannelSftp connect(String host, int port, String username, String password) {
        ChannelSftp sftp = null;
        JSch jsch = new JSch();
        try {
            jsch.getSession(username, host, port);
            session = jsch.getSession(username, host, port);
            session.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            session.setConfig(sshConfig);
            session.connect();
            System.out.println("session connected!");
            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
        } catch (JSchException e) {
            System.out.println("获取SFTP连接异常"+e.getMessage());
        }
        return sftp;
    }

    // 2.打开或者进入指定目录
    public static boolean openDir(String directory, ChannelSftp sftp) {
        try {
            sftp.cd(directory);
            return true;
        } catch (SftpException e) {
            System.out.println(e + "");
            return false;
        }
    }

    // 3.关闭资源
    public static void close() {
        if (channel != null) {
            channel.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }

    // 4.遍历一个目录,并得到这个路径的集合list(等递归把这个目录下遍历结束后list存放的就是这个目录下的所有文件的路径集合)
    public static List<String> getSftpPathList(String pathName, ChannelSftp sftp) throws IOException, SftpException {
        boolean flag = openDir(pathName, sftp);
        if (flag) {
            Vector vv = sftp.ls(pathName);
            if (vv == null && vv.size() == 0) {
                return null;
            } else {
                for (Object object : vv) {
                    ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) object;
                    String filename = entry.getFilename();
                    if (".".equals(filename) || "..".equals(filename)) {
                        continue;
                    }
                    if (openDir(pathName + filename + "/", sftp)) {
                        // 能打开,说明是目录,接着遍历
                        getSftpPathList(pathName + filename + "/", sftp);
                    } else {
                        sftpList.add(pathName + filename);
                    }
                }
            }
        } else {
            System.out.println("对应的目录不存在!");
        }
        if (sftpList != null && sftpList.size() > 0) {
        }
        return sftpList;
    }

}

具体使用:

ChannelSftp channel=SftpUtils.connect("具体的服务器ip", int 端口号,"用户名", "密码");
        try {
            List<String> list=SftpUtils.getSftpPathList("/root/ident/upload/img/",channel);//遍历的路径
            System.out.println(list.size());

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值