public class SshjSftp extends AbstractFtp
基于sshj 框架适配。
参考:https://github.com/hierynomus/sshj
ftpConfigDEFAULT_CHARSET| Constructor and Description |
|---|
SshjSftp(FtpConfig config)
构造
|
SshjSftp(net.schmizz.sshj.SSHClient sshClient,
Charset charset)
构造
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
cd(String directory)
打开指定目录,具体逻辑取决于实现,例如在FTP中,进入失败返回
false, SFTP中则抛出异常 |
void |
close() |
String |
command(String exec)
执行Linux 命令
|
boolean |
containsFile(String fileDir)
是否包含该文件
|
boolean |
delDir(String dirPath)
删除文件夹及其文件夹下的所有文件
|
boolean |
delFile(String path)
删除指定目录下的指定文件
|
void |
download(String path,
File outFile)
下载文件
|
InputStream |
getFileStream(String path)
读取远程文件输入流
|
void |
init()
SSH 初始化并创建一个sftp客户端.
|
List<String> |
ls(String path)
遍历某个目录下所有文件和目录,不会递归遍历
|
boolean |
mkdir(String dir)
在当前远程目录(工作目录)下创建新的目录
|
static SshjSftp |
of(String sshHost,
int sshPort,
String sshUser,
String sshPass)
构造
|
static SshjSftp |
of(String sshHost,
int sshPort,
String sshUser,
String sshPass,
Charset charset)
构造
|
static SshjSftp |
of(String sshHost,
String sshUser,
String sshPass)
构造
|
String |
pwd()
远程当前目录(工作目录)
|
AbstractFtp |
reconnectIfTimeout()
如果连接超时的话,重新进行连接
|
void |
recursiveDownloadFolder(String sourcePath,
File destDir)
递归下载FTP服务器上文件到本地(文件目录和服务器同步), 服务器上有新文件会覆盖本地文件
|
boolean |
uploadFile(String destPath,
File file)
将本地文件上传到目标服务器,目标文件名为destPath,若destPath为目录,则目标文件名将与file文件名相同。
|
download, exist, getConfig, mkDirspublic SshjSftp(FtpConfig config)
config - FTP配置public SshjSftp(net.schmizz.sshj.SSHClient sshClient,
Charset charset)
sshClient - SSHClientcharset - 编码public static SshjSftp of(String sshHost, String sshUser, String sshPass)
sshHost - 主机sshUser - 用户名sshPass - 密码public static SshjSftp of(String sshHost, int sshPort, String sshUser, String sshPass)
sshHost - 主机sshPort - 端口sshUser - 用户名sshPass - 密码public static SshjSftp of(String sshHost, int sshPort, String sshUser, String sshPass, Charset charset)
sshHost - 主机sshPort - 端口sshUser - 用户名sshPass - 密码charset - 编码public void init()
public AbstractFtp reconnectIfTimeout()
Ftppublic boolean cd(String directory)
Ftpfalse, SFTP中则抛出异常directory - directorypublic boolean mkdir(String dir)
Ftpdir - 目录名public List<String> ls(String path)
Ftppath - 需要遍历的目录public boolean delFile(String path)
Ftppath - 目录路径public boolean delDir(String dirPath)
FtpdirPath - 文件夹路径public boolean uploadFile(String destPath, File file)
FtpdestPath - 服务端路径,可以为null 或者相对路径或绝对路径file - 需要上传的文件public void download(String path, File outFile)
Ftppath - 文件路径outFile - 输出文件或目录public void recursiveDownloadFolder(String sourcePath, File destDir)
FtpsourcePath - ftp服务器目录destDir - 本地目录public InputStream getFileStream(String path)
path - 远程文件路径InputStreampublic void close()
public boolean containsFile(String fileDir)
fileDir - 文件绝对路径Copyright © 2025. All rights reserved.