java.lang.Object
org.miaixz.bus.extra.ftp.AbstractFtp
org.miaixz.bus.extra.ssh.provider.sshj.SshjSftp
- All Implemented Interfaces:
Closeable,AutoCloseable,Ftp
An SFTP client implementation based on the SSHJ library. This class was created to address issues with character
encoding (e.g., garbled Chinese characters) when using other libraries like JSch for SFTP file downloads, as SSHJ
provides better control over character sets.
Adapted from the SSHJ library. For more details, see the project homepage: https://github.com/hierynomus/sshj
- Since:
- Java 17+
- Author:
- Kimi Liu
-
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanChanges the current working directory on the remote FTP server to the specified directory.voidclose()Executes a command on the remote server.booleancontainsFile(String fileDir) Checks if the specified file exists on the remote server.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.getFileStream(String path) Reads a file from the FTP server and provides its content as anInputStream.voidinit()Initializes the SSH connection and creates an SFTP client.Lists all file and directory names within a specified remote directory (non-recursive).booleanCreates a new directory in the current remote working directory.static SshjSftpCreates anSshjSftpinstance with the specified SSH host, port, username, and password.static SshjSftpCreates anSshjSftpinstance with the specified SSH host, port, username, password, and character set.static SshjSftpCreates anSshjSftpinstance with the specified SSH host, username, and password, using the default port (22).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 destPath, 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.booleanuploadFile(String destPath, File file) Uploads a local file to the target server.Methods inherited from class org.miaixz.bus.extra.ftp.AbstractFtp
download, exist, getConfig, mkDirs
-
Constructor Details
-
SshjSftp
Constructs anSshjSftpinstance with the given FTP configuration and initializes it.- Parameters:
config- TheFtpConfigcontaining connection details.
-
SshjSftp
Constructs anSshjSftpinstance with an existingSSHClientand character set, and initializes it.- Parameters:
sshClient- The pre-configuredSSHClientinstance.charset- The character set for file names.
-
-
Method Details
-
of
Creates anSshjSftpinstance with the specified SSH host, username, and password, using the default port (22).- Parameters:
sshHost- The SSH host address.sshUser- The SSH username.sshPass- The SSH password.- Returns:
- A new
SshjSftpinstance.
-
of
Creates anSshjSftpinstance with the specified SSH host, port, username, and password.- Parameters:
sshHost- The SSH host address.sshPort- The SSH port.sshUser- The SSH username.sshPass- The SSH password.- Returns:
- A new
SshjSftpinstance.
-
of
public static SshjSftp of(String sshHost, int sshPort, String sshUser, String sshPass, Charset charset) Creates anSshjSftpinstance with the specified SSH host, port, username, password, and character set.- Parameters:
sshHost- The SSH host address.sshPort- The SSH port.sshUser- The SSH username.sshPass- The SSH password.charset- The character set for file names.- Returns:
- A new
SshjSftpinstance.
-
init
public void init()Initializes the SSH connection and creates an SFTP client. If the SSH client is not already initialized, it will be created and connected.- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if SFTP initialization fails due to an I/O error.
-
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.
-
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.
-
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.
-
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.
-
ls
Description copied from interface:FtpLists all file and directory names within a specified remote directory (non-recursive). -
delFile
Description copied from interface:FtpDeletes a specified file on the remote FTP server.- Parameters:
path- 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.
-
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.
-
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:
destPath- The path to the file on the remote FTP server.outFile- The localFileor directory where the downloaded file should be saved.
-
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:
destPath- The source directory on the FTP server to download from.targetDir- The target directory on the local machine where files will be saved.
-
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() -
containsFile
Checks if the specified file exists on the remote server.- Parameters:
fileDir- The absolute path of the file.- Returns:
trueif the file exists,falseotherwise.
-
command
Executes a command on the remote server.- Parameters:
exec- The command to execute.- Returns:
- The command's output as a string.
- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an error occurs during command execution.
-