public class JschSftp extends AbstractFtp
此类为基于jsch的SFTP实现
参考:https://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html
| Modifier and Type | Class and Description |
|---|---|
static class |
JschSftp.Mode
JSch支持的三种文件传输模式
|
ftpConfigDEFAULT_CHARSET| Constructor and Description |
|---|
JschSftp(com.jcraft.jsch.ChannelSftp channel,
Charset charset,
long timeOut)
构造
|
JschSftp(FtpConfig config)
构造
|
JschSftp(FtpConfig config,
boolean init)
构造
|
JschSftp(com.jcraft.jsch.Session session,
Charset charset,
long timeOut)
构造
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
cd(String directory)
打开指定目录,如果指定路径非目录或不存在抛出异常
|
void |
close() |
boolean |
delDir(String dirPath)
删除文件夹及其文件夹下的所有文件
|
boolean |
delFile(String filePath)
删除文件
|
void |
download(String src,
File destFile)
下载文件
|
void |
download(String src,
OutputStream out)
下载文件到
OutputStream中 |
JschSftp |
get(String src,
OutputStream out)
获取远程文件
|
JschSftp |
get(String src,
String dest)
获取远程文件
|
com.jcraft.jsch.ChannelSftp |
getClient()
获取SFTP通道客户端
|
InputStream |
getFileStream(String path)
读取FTP服务器上的文件为输入流
|
String |
home()
获取HOME路径
|
void |
init()
初始化
|
boolean |
isDir(String dir)
判断给定路径是否为目录
|
List<String> |
ls(String path)
遍历某个目录下所有文件或目录,不会递归遍历
|
List<String> |
ls(String path,
Predicate<com.jcraft.jsch.ChannelSftp.LsEntry> predicate)
遍历某个目录下所有文件或目录,不会递归遍历
此方法自动过滤"." |
List<String> |
lsDirs(String path)
遍历某个目录下所有目录,不会递归遍历
|
List<com.jcraft.jsch.ChannelSftp.LsEntry> |
lsEntries(String path)
遍历某个目录下所有文件或目录,生成LsEntry列表,不会递归遍历
此方法自动过滤"." |
List<com.jcraft.jsch.ChannelSftp.LsEntry> |
lsEntries(String path,
Predicate<com.jcraft.jsch.ChannelSftp.LsEntry> predicate)
遍历某个目录下所有文件或目录,生成LsEntry列表,不会递归遍历
此方法自动过滤"." |
List<String> |
lsFiles(String path)
遍历某个目录下所有文件,不会递归遍历
|
boolean |
mkdir(String dir)
在当前远程目录(工作目录)下创建新的目录
|
static JschSftp |
of(String sshHost,
int sshPort,
String sshUser,
String sshPass)
构造
|
static JschSftp |
of(String sshHost,
int sshPort,
String sshUser,
String sshPass,
Charset charset)
构造
|
JschSftp |
put(InputStream srcStream,
String destPath,
com.jcraft.jsch.SftpProgressMonitor monitor,
JschSftp.Mode mode)
将本地数据流上传到目标服务器,目标文件名为destPath,目标必须为文件
|
JschSftp |
put(String srcFilePath,
String destPath)
将本地文件上传到目标服务器,目标文件名为destPath,若destPath为目录,则目标文件名将与srcFilePath文件名相同。
|
JschSftp |
put(String srcFilePath,
String destPath,
JschSftp.Mode mode)
将本地文件上传到目标服务器,目标文件名为destPath,若destPath为目录,则目标文件名将与srcFilePath文件名相同。
|
JschSftp |
put(String srcFilePath,
String destPath,
com.jcraft.jsch.SftpProgressMonitor monitor,
JschSftp.Mode mode)
将本地文件上传到目标服务器,目标文件名为destPath,若destPath为目录,则目标文件名将与srcFilePath文件名相同。
|
String |
pwd()
远程当前目录
|
JschSftp |
reconnectIfTimeout()
如果连接超时的话,重新进行连接
|
void |
recursiveDownloadFolder(String sourcePath,
File destDir)
递归下载FTP服务器上文件到本地(文件目录和服务器同步)
|
String |
toString() |
void |
upload(String remotePath,
File file)
将本地文件或者文件夹同步(覆盖)上传到远程路径
|
boolean |
uploadFile(String destPath,
File file)
将本地文件上传到目标服务器,目标文件名为destPath,若destPath为目录,则目标文件名将与file文件名相同。
|
void |
uploadFile(String destPath,
String fileName,
InputStream fileStream)
上传文件到指定目录,可选:
1. path为null或""上传到当前路径
2. path为相对路径则相对于当前路径的子路径
3. path为绝对路径则上传到此路径
|
download, exist, getConfig, mkDirspublic JschSftp(FtpConfig config)
config - FTP配置public JschSftp(FtpConfig config, boolean init)
config - FTP配置init - 是否立即初始化public JschSftp(com.jcraft.jsch.Session session,
Charset charset,
long timeOut)
session - Sessioncharset - 编码timeOut - 超时时间,单位毫秒public JschSftp(com.jcraft.jsch.ChannelSftp channel,
Charset charset,
long timeOut)
channel - ChannelSftpcharset - 编码timeOut - 超时时间,单位毫秒public static JschSftp of(String sshHost, int sshPort, String sshUser, String sshPass)
sshHost - 远程主机sshPort - 远程主机端口sshUser - 远程主机用户名sshPass - 远程主机密码public static JschSftp of(String sshHost, int sshPort, String sshUser, String sshPass, Charset charset)
sshHost - 远程主机sshPort - 远程主机端口sshUser - 远程主机用户名sshPass - 远程主机密码charset - 编码public void init()
public JschSftp reconnectIfTimeout()
Ftppublic com.jcraft.jsch.ChannelSftp getClient()
public String pwd()
public String home()
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, Predicate<com.jcraft.jsch.ChannelSftp.LsEntry> predicate)
path - 遍历某个目录下所有文件或目录predicate - 文件或目录过滤器,可以实现过滤器返回自己需要的文件或目录名列表,Predicate.test(Object)为true保留public List<com.jcraft.jsch.ChannelSftp.LsEntry> lsEntries(String path)
path - 遍历某个目录下所有文件或目录public List<com.jcraft.jsch.ChannelSftp.LsEntry> lsEntries(String path, Predicate<com.jcraft.jsch.ChannelSftp.LsEntry> predicate)
path - 遍历某个目录下所有文件或目录predicate - 文件或目录过滤器,可以实现过滤器返回自己需要的文件或目录名列表,Predicate.test(Object)为true保留public boolean mkdir(String dir)
Ftpdir - 目录名public boolean isDir(String dir)
Ftpdir - 被判断的路径public boolean cd(String directory) throws FtpException
directory - directoryFtpException - 进入目录失败异常public boolean delFile(String filePath)
filePath - 要删除的文件绝对路径public boolean delDir(String dirPath)
dirPath - 文件夹路径public void upload(String remotePath, File file)
remotePath - 远程路径file - 文件或者文件夹public boolean uploadFile(String destPath, File file)
FtpdestPath - 服务端路径,可以为null 或者相对路径或绝对路径file - 需要上传的文件public void uploadFile(String destPath, String fileName, InputStream fileStream)
1. path为null或""上传到当前路径 2. path为相对路径则相对于当前路径的子路径 3. path为绝对路径则上传到此路径
destPath - 服务端路径,可以为null 或者相对路径或绝对路径fileName - 文件名fileStream - 文件流public JschSftp put(String srcFilePath, String destPath)
srcFilePath - 本地文件路径destPath - 目标路径,public JschSftp put(String srcFilePath, String destPath, JschSftp.Mode mode)
srcFilePath - 本地文件路径destPath - 目标路径,mode - JschSftp.Mode 模式public JschSftp put(String srcFilePath, String destPath, com.jcraft.jsch.SftpProgressMonitor monitor, JschSftp.Mode mode)
srcFilePath - 本地文件路径destPath - 目标路径,monitor - 上传进度监控,通过实现此接口完成进度显示mode - JschSftp.Mode 模式public JschSftp put(InputStream srcStream, String destPath, com.jcraft.jsch.SftpProgressMonitor monitor, JschSftp.Mode mode)
srcStream - 本地的数据流destPath - 目标路径,monitor - 上传进度监控,通过实现此接口完成进度显示mode - JschSftp.Mode 模式public void download(String src, File destFile)
Ftpsrc - 文件路径destFile - 输出文件或目录public void download(String src, OutputStream out)
OutputStream中src - 源文件路径,包括文件名out - 目标流get(String, OutputStream)public void recursiveDownloadFolder(String sourcePath, File destDir) throws SshException
sourcePath - ftp服务器目录,必须为目录destDir - 本地目录SshExceptionpublic JschSftp get(String src, String dest)
src - 远程文件路径dest - 目标文件路径public JschSftp get(String src, OutputStream out)
src - 远程文件路径out - 目标流public InputStream getFileStream(String path)
Ftppath - 文件路径InputStreampublic void close()
Copyright © 2025. All rights reserved.