public class Sftp extends Object implements Closeable
此类为基于jsch的SFTP实现
参考:https://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html
| 限定符和类型 | 类和说明 |
|---|---|
static class |
Sftp.Mode
JSch支持的三种文件传输模式
|
| 构造器和说明 |
|---|
Sftp(com.jcraft.jsch.ChannelSftp channel)
构造
|
Sftp(com.jcraft.jsch.Session session)
构造
|
Sftp(String sshHost,
int sshPort,
String sshUser,
String sshPass)
构造
|
| 限定符和类型 | 方法和说明 |
|---|---|
boolean |
cd(String directory)
打开指定目录
|
void |
close() |
boolean |
delDir(String dirPath)
删除文件夹及其文件夹下的所有文件
|
Sftp |
delFile(String filePath)
删除文件
|
boolean |
exist(String path,
String fileOrDirName)
文件或目录在指定目录下是否存在
|
Sftp |
get(String src,
String dest)
获取远程文件
|
com.jcraft.jsch.ChannelSftp |
getChannel()
获取SFTP通道
|
String |
home()
获取HOME路径
|
List<String> |
ls(String path)
遍历某个目录下所有文件或目录,不会递归遍历
|
List<String> |
ls(String path,
Filter<com.jcraft.jsch.ChannelSftp.LsEntry> filter)
遍历某个目录下所有文件或目录,不会递归遍历
|
List<String> |
lsDirs(String path)
遍历某个目录下所有目录,不会递归遍历
|
List<String> |
lsFiles(String path)
遍历某个目录下所有文件,不会递归遍历
|
void |
mkDirs(String dir)
创建指定文件夹及其父目录
|
Sftp |
put(String srcFilePath,
String destPath)
将本地文件上传到目标服务器,目标文件名为destPath,若destPath为目录,则目标文件名将与srcFilePath文件名相同。
|
Sftp |
put(String srcFilePath,
String destPath,
Sftp.Mode mode)
将本地文件上传到目标服务器,目标文件名为destPath,若destPath为目录,则目标文件名将与srcFilePath文件名相同。
|
String |
pwd()
远程当前目录
|
boolean |
toParent()
打开上级目录
|
public Sftp(String sshHost, int sshPort, String sshUser, String sshPass)
sshHost - 远程主机sshPort - 远程主机端口sshUser - 远程主机用户名sshPass - 远程主机密码public Sftp(com.jcraft.jsch.Session session)
session - Sessionpublic Sftp(com.jcraft.jsch.ChannelSftp channel)
channel - ChannelSftppublic com.jcraft.jsch.ChannelSftp getChannel()
public String pwd()
public String home()
public boolean exist(String path, String fileOrDirName)
path - 目录fileOrDirName - 文件或目录名public List<String> ls(String path)
path - 遍历某个目录下所有文件或目录public List<String> lsDirs(String path)
path - 遍历某个目录下所有目录public List<String> lsFiles(String path)
path - 遍历某个目录下所有文件public List<String> ls(String path, Filter<com.jcraft.jsch.ChannelSftp.LsEntry> filter)
path - 遍历某个目录下所有文件或目录filter - 文件或目录过滤器,可以实现过滤器返回自己需要的文件或目录名列表public boolean toParent()
public boolean cd(String directory)
directory - directorypublic void mkDirs(String dir)
dir - 文件夹路径,绝对路径public boolean delDir(String dirPath)
dirPath - 文件夹路径public Sftp put(String srcFilePath, String destPath)
srcFilePath - 本地文件路径destPath - 目标路径,public Sftp put(String srcFilePath, String destPath, Sftp.Mode mode)
srcFilePath - 本地文件路径destPath - 目标路径,mode - Sftp.Mode 模式public void close()
throws IOException
close 在接口中 Closeableclose 在接口中 AutoCloseableIOExceptionCopyright © 2018. All rights reserved.