需要使用的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());