public class SFTPUtil extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
private String |
host
SFTP 服务器地址IP地址
|
private org.slf4j.Logger |
log |
private String |
password
SFTP 登录密码
|
private int |
port
SFTP 端口
|
private String |
privateKey
私钥
|
private com.jcraft.jsch.Session |
session |
private com.jcraft.jsch.ChannelSftp |
sftp |
private String |
username
SFTP 登录用户名
|
| 构造器和说明 |
|---|
SFTPUtil() |
SFTPUtil(String username,
String host,
int port,
String privateKey)
构造基于秘钥认证的sftp对象
|
SFTPUtil(String username,
String password,
String host,
int port)
构造基于密码认证的sftp对象
|
| 限定符和类型 | 方法和说明 |
|---|---|
void |
delete(String directory,
String deleteFile)
删除文件
|
byte[] |
download(String directory,
String downloadFile)
下载文件
|
void |
download(String directory,
String downloadFile,
String saveFile)
下载文件。
|
Vector<?> |
listFiles(String directory)
列出目录下的文件
|
void |
login()
连接sftp服务器
|
void |
logout()
关闭连接 server
|
void |
upload(String basePath,
String directory,
String sftpFileName,
InputStream input)
将输入流的数据上传到sftp作为文件。
|
private transient org.slf4j.Logger log
private com.jcraft.jsch.ChannelSftp sftp
private com.jcraft.jsch.Session session
private String username
private String password
private String privateKey
private String host
private int port
public void login()
public void logout()
public void upload(String basePath, String directory, String sftpFileName, InputStream input) throws com.jcraft.jsch.SftpException
basePath - 服务器的基础路径directory - 上传到该目录sftpFileName - sftp端文件名input - 输入流com.jcraft.jsch.SftpExceptionpublic void download(String directory, String downloadFile, String saveFile) throws com.jcraft.jsch.SftpException, FileNotFoundException
directory - 下载目录downloadFile - 下载的文件saveFile - 存在本地的路径com.jcraft.jsch.SftpExceptionFileNotFoundExceptionpublic byte[] download(String directory, String downloadFile) throws com.jcraft.jsch.SftpException, IOException
directory - 下载目录downloadFile - 下载的文件名com.jcraft.jsch.SftpExceptionIOExceptionpublic void delete(String directory, String deleteFile) throws com.jcraft.jsch.SftpException
directory - 要删除文件所在目录deleteFile - 要删除的文件com.jcraft.jsch.SftpExceptionCopyright © 2020. All rights reserved.