java.lang.Object
org.miaixz.bus.extra.ftp.AbstractFtp
org.miaixz.bus.extra.ssh.provider.jsch.JschSftp
- All Implemented Interfaces:
Closeable,AutoCloseable,Ftp
SFTP (Secure File Transfer Protocol) client implementation based on JSch. SFTP provides a secure, encrypted method
for file transfer over SSH, making it a robust alternative to traditional FTP. Due to encryption and decryption
overhead, its transfer efficiency is generally lower than standard FTP. This class is suitable for scenarios with
high network security requirements.
Reference: https://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnumeration of file transfer modes supported by JSch. -
Field Summary
Fields inherited from class org.miaixz.bus.extra.ftp.AbstractFtp
ftpConfigFields inherited from interface org.miaixz.bus.extra.ftp.Ftp
DEFAULT_CHARSET -
Constructor Summary
ConstructorsConstructorDescriptionConstructs aJschSftpinstance with an existing JSchChannelSftp, character set, and timeout.Constructs aJschSftpinstance with an existing JSchSession, character set, and timeout.Constructs aJschSftpinstance with the given FTP configuration and initializes it immediately.Constructs aJschSftpinstance with the given FTP configuration and an option to initialize immediately. -
Method Summary
Modifier and TypeMethodDescriptionbooleanChanges the current working directory on the remote FTP server to the specified directory.voidclose()booleanDeletes a directory and all its contents (files and subdirectories) recursively on the remote FTP server.booleanDeletes a specified file on the remote FTP server.voidDownloads a file from the remote FTP server to a specified local file or directory.voiddownload(String src, OutputStream out) Downloads a file to anOutputStream.get(String src, OutputStream out) Retrieves a remote file and writes it to an output stream.Retrieves a remote file and saves it to a local path.com.jcraft.jsch.ChannelSftpRetrieves the underlying SFTP channel client.getFileStream(String path) Reads a file from the FTP server and provides its content as anInputStream.home()Retrieves the HOME directory on the SFTP server.voidinit()Initializes the SFTP connection.booleanDetermines if the given path on the remote server refers to a directory.Lists all file and directory names within a specified remote directory (non-recursive).Lists all files or directories in a given path, without recursion, applying a filter.Lists all directories in a given path, without recursion.List<com.jcraft.jsch.ChannelSftp.LsEntry> Lists all files or directories in a given path, generating a list ofChannelSftp.LsEntryobjects, without recursion.List<com.jcraft.jsch.ChannelSftp.LsEntry> Lists all files or directories in a given path, generating a list ofChannelSftp.LsEntryobjects, without recursion, applying a filter.Lists all files in a given path, without recursion.booleanCreates a new directory in the current remote working directory.static JschSftpCreates aJschSftpinstance with the specified SSH connection details.static JschSftpCreates aJschSftpinstance with the specified SSH connection details and character set.put(InputStream srcStream, String destPath, com.jcraft.jsch.SftpProgressMonitor monitor, JschSftp.Mode mode) Uploads a local data stream to the target server.Uploads a local file to the target server, overwriting existing files.put(String srcFilePath, String destPath, com.jcraft.jsch.SftpProgressMonitor monitor, JschSftp.Mode mode) Uploads a local file to the target server with a progress monitor and transfer mode.put(String srcFilePath, String destPath, JschSftp.Mode mode) Uploads a local file to the target server with a specified transfer mode.pwd()Retrieves the current remote directory (working directory) on the FTP server.Reconnects to the FTP server if the current connection has timed out or become stale.voidrecursiveDownloadFolder(String remotePath, File targetDir) Recursively downloads files and directories from the FTP server to the local machine, synchronizing the file structures.booleanRenames a file or directory on the remote FTP server.toString()voidRecursively uploads a local file or folder to a remote path, overwriting existing files.booleanuploadFile(String destPath, File file) Uploads a local file to the target server.voiduploadFile(String destPath, String fileName, InputStream fileStream) Uploads a file from an InputStream to the specified remote directory.Methods inherited from class org.miaixz.bus.extra.ftp.AbstractFtp
download, exist, getConfig, mkDirs
-
Constructor Details
-
JschSftp
Constructs aJschSftpinstance with the given FTP configuration and initializes it immediately.- Parameters:
config- TheFtpConfigcontaining connection details.
-
JschSftp
Constructs aJschSftpinstance with the given FTP configuration and an option to initialize immediately.- Parameters:
config- TheFtpConfigcontaining connection details.init- Iftrue, the SFTP connection is initialized immediately.
-
JschSftp
Constructs aJschSftpinstance with an existing JSchSession, character set, and timeout.- Parameters:
session- The pre-existing JSchSession.charset- The character set for file names.timeOut- The connection timeout duration in milliseconds.
-
JschSftp
Constructs aJschSftpinstance with an existing JSchChannelSftp, character set, and timeout.- Parameters:
channel- The pre-existing JSchChannelSftp.charset- The character set for file names.timeOut- The connection timeout duration in milliseconds.
-
-
Method Details
-
of
Creates aJschSftpinstance with the specified SSH connection details.- Parameters:
sshHost- The remote host address.sshPort- The remote host port.sshUser- The username for authentication.sshPass- The password for authentication.- Returns:
- A new
JschSftpinstance.
-
of
public static JschSftp of(String sshHost, int sshPort, String sshUser, String sshPass, Charset charset) Creates aJschSftpinstance with the specified SSH connection details and character set.- Parameters:
sshHost- The remote host address.sshPort- The remote host port.sshUser- The username for authentication.sshPass- The password for authentication.charset- The character set for file names.- Returns:
- A new
JschSftpinstance.
-
init
public void init()Initializes the SFTP connection. If the session or channel is not yet established, it will be created and connected. The filename encoding will be set based on the FTP configuration.- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if a JSch or SftpException occurs during initialization.
-
reconnectIfTimeout
Description copied from interface:FtpReconnects to the FTP server if the current connection has timed out or become stale. Implementations should handle the logic for checking connection validity and re-establishing it.- Returns:
- This
Ftpinstance, allowing for method chaining.
-
getClient
public com.jcraft.jsch.ChannelSftp getClient()Retrieves the underlying SFTP channel client.- Returns:
- The
ChannelSftpclient. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an error occurs during initialization or connection.
-
pwd
Description copied from interface:FtpRetrieves the current remote directory (working directory) on the FTP server.- Returns:
- The absolute path of the current working directory as a
String.
-
home
Retrieves the HOME directory on the SFTP server.- Returns:
- The HOME directory path.
- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an SftpException occurs.
-
ls
Description copied from interface:FtpLists all file and directory names within a specified remote directory (non-recursive). -
lsDirs
Lists all directories in a given path, without recursion.- Parameters:
path- The path to list directories from.- Returns:
- A list of directory names.
-
lsFiles
Lists all files in a given path, without recursion.- Parameters:
path- The path to list files from.- Returns:
- A list of file names.
-
ls
Lists all files or directories in a given path, without recursion, applying a filter. This method automatically filters out "." and ".." directories.- Parameters:
path- The path to list files or directories from.predicate- A file or directory filter.Predicate.test(Object)returningtruekeeps the entry.- Returns:
- A list of directory or file names.
- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an SftpException occurs other than "No such file".
-
lsEntries
Lists all files or directories in a given path, generating a list ofChannelSftp.LsEntryobjects, without recursion. This method automatically filters out "." and ".." directories.- Parameters:
path- The path to list files or directories from.- Returns:
- A list of
ChannelSftp.LsEntryobjects.
-
lsEntries
public List<com.jcraft.jsch.ChannelSftp.LsEntry> lsEntries(String path, Predicate<com.jcraft.jsch.ChannelSftp.LsEntry> predicate) Lists all files or directories in a given path, generating a list ofChannelSftp.LsEntryobjects, without recursion, applying a filter. This method automatically filters out "." and ".." directories.- Parameters:
path- The path to list files or directories from.predicate- A file or directory filter.Predicate.test(Object)returningtruekeeps the entry.- Returns:
- A list of
ChannelSftp.LsEntryobjects. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an SftpException occurs other than "No such file".
-
rename
Description copied from interface:FtpRenames a file or directory on the remote FTP server.- Parameters:
oldPath- The current path or name of the file/directory to rename.newPath- The new path or name for the file/directory.- Returns:
trueif the rename operation was successful;falseotherwise.
-
mkdir
Description copied from interface:FtpCreates a new directory in the current remote working directory.- Parameters:
dir- The name of the directory to create.- Returns:
trueif the directory was created successfully;falseotherwise.
-
isDir
Description copied from interface:FtpDetermines if the given path on the remote server refers to a directory. This method temporarily changes the directory to the given path to verify if it's a directory, then reverts to the original working directory.- Parameters:
dir- The path to check.- Returns:
trueif the path is a directory;falseotherwise.
-
cd
Description copied from interface:FtpChanges the current working directory on the remote FTP server to the specified directory. The behavior for invalid directories (e.g., non-existent) may vary between implementations.- Parameters:
directory- The path to the directory to change to.- Returns:
trueif the directory was successfully changed;falseotherwise.- Throws:
org.miaixz.bus.core.lang.exception.InternalException
-
delFile
Description copied from interface:FtpDeletes a specified file on the remote FTP server.- Parameters:
filePath- The path to the file to delete.- Returns:
trueif the file was deleted successfully;falseotherwise.
-
delDir
Description copied from interface:FtpDeletes a directory and all its contents (files and subdirectories) recursively on the remote FTP server.- Parameters:
dirPath- The path to the directory to delete.- Returns:
trueif the directory and its contents were deleted successfully;falseotherwise.
-
upload
Recursively uploads a local file or folder to a remote path, overwriting existing files.- Parameters:
remotePath- The remote path.file- The local file or folder to upload.
-
uploadFile
Description copied from interface:FtpUploads a local file to the target server. The destination path on the server can be specified. IfdestPathis a directory, the file will be uploaded with its original name into that directory. This operation typically overwrites existing files with the same name.- Parameters:
destPath- The destination path on the server. Can benull(uploads to current working directory), a relative path, or an absolute path. If it's a directory, the file's original name is used.file- The localFileobject to upload.- Returns:
trueif the upload was successful;falseotherwise.
-
uploadFile
Uploads a file from an InputStream to the specified remote directory.- Parameters:
destPath- The destination path on the server.fileName- The filename.fileStream- The file input stream.- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an SftpException occurs.
-
put
Uploads a local file to the target server, overwriting existing files.- Parameters:
srcFilePath- The local file path.destPath- The target path.- Returns:
- This
JschSftpinstance. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an SftpException occurs.
-
put
Uploads a local file to the target server with a specified transfer mode.- Parameters:
srcFilePath- The local file path.destPath- The target path.mode- TheJschSftp.Modefor file transfer.- Returns:
- This
JschSftpinstance. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an SftpException occurs.
-
put
public JschSftp put(String srcFilePath, String destPath, com.jcraft.jsch.SftpProgressMonitor monitor, JschSftp.Mode mode) Uploads a local file to the target server with a progress monitor and transfer mode.- Parameters:
srcFilePath- The local file path.destPath- The target path.monitor- The upload progress monitor.mode- TheJschSftp.Modefor file transfer.- Returns:
- This
JschSftpinstance. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an SftpException occurs.
-
put
public JschSftp put(InputStream srcStream, String destPath, com.jcraft.jsch.SftpProgressMonitor monitor, JschSftp.Mode mode) Uploads a local data stream to the target server.- Parameters:
srcStream- The local data stream.destPath- The target path.monitor- The upload progress monitor.mode- TheJschSftp.Modefor file transfer.- Returns:
- This
JschSftpinstance. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an SftpException occurs.
-
download
Description copied from interface:FtpDownloads a file from the remote FTP server to a specified local file or directory. IfoutFileis a directory, the downloaded file will be saved into it with its original name.- Parameters:
remotePath- The path to the file on the remote FTP server.destFile- The localFileor directory where the downloaded file should be saved.
-
download
Downloads a file to anOutputStream.- Parameters:
src- The source file path on the remote server.out- The target output stream.
-
recursiveDownloadFolder
Description copied from interface:FtpRecursively downloads files and directories from the FTP server to the local machine, synchronizing the file structures. Existing local files will be overwritten by newer files from the server.- Parameters:
remotePath- The source directory on the FTP server to download from.targetDir- The target directory on the local machine where files will be saved.
-
get
Retrieves a remote file and saves it to a local path.- Parameters:
src- The remote file path.dest- The target file path.- Returns:
- This
JschSftpinstance. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an SftpException occurs.
-
get
Retrieves a remote file and writes it to an output stream.- Parameters:
src- The remote file path.out- The target output stream.- Returns:
- This
JschSftpinstance. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an SftpException occurs.
-
getFileStream
Description copied from interface:FtpReads a file from the FTP server and provides its content as anInputStream. This allows for streaming processing of remote file content.- Parameters:
path- The path to the file on the FTP server.- Returns:
- An
InputStreamproviding access to the file's content.
-
close
public void close() -
toString
-