Module bus.extra

Class SshjSftp

java.lang.Object
org.miaixz.bus.extra.ftp.AbstractFtp
org.miaixz.bus.extra.ssh.provider.sshj.SshjSftp
All Implemented Interfaces:
Closeable, AutoCloseable, Ftp

public class SshjSftp extends AbstractFtp
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

    ftpConfig

    Fields inherited from interface org.miaixz.bus.extra.ftp.Ftp

    DEFAULT_CHARSET
  • Constructor Summary

    Constructors
    Constructor
    Description
    SshjSftp(net.schmizz.sshj.SSHClient sshClient, Charset charset)
    Constructs an SshjSftp instance with an existing SSHClient and character set, and initializes it.
    Constructs an SshjSftp instance with the given FTP configuration and initializes it.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    cd(String directory)
    Changes the current working directory on the remote FTP server to the specified directory.
    void
     
    Executes a command on the remote server.
    boolean
    Checks if the specified file exists on the remote server.
    boolean
    delDir(String dirPath)
    Deletes a directory and all its contents (files and subdirectories) recursively on the remote FTP server.
    boolean
    Deletes a specified file on the remote FTP server.
    void
    download(String destPath, File outFile)
    Downloads a file from the remote FTP server to a specified local file or directory.
    Reads a file from the FTP server and provides its content as an InputStream.
    void
    Initializes the SSH connection and creates an SFTP client.
    ls(String path)
    Lists all file and directory names within a specified remote directory (non-recursive).
    boolean
    Creates a new directory in the current remote working directory.
    static SshjSftp
    of(String sshHost, int sshPort, String sshUser, String sshPass)
    Creates an SshjSftp instance with the specified SSH host, port, username, and password.
    static SshjSftp
    of(String sshHost, int sshPort, String sshUser, String sshPass, Charset charset)
    Creates an SshjSftp instance with the specified SSH host, port, username, password, and character set.
    static SshjSftp
    of(String sshHost, String sshUser, String sshPass)
    Creates an SshjSftp instance 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.
    void
    recursiveDownloadFolder(String destPath, File targetDir)
    Recursively downloads files and directories from the FTP server to the local machine, synchronizing the file structures.
    boolean
    rename(String oldPath, String newPath)
    Renames a file or directory on the remote FTP server.
    boolean
    uploadFile(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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.miaixz.bus.extra.ftp.Ftp

    isDir, toParent
  • Constructor Details

    • SshjSftp

      public SshjSftp(FtpConfig config)
      Constructs an SshjSftp instance with the given FTP configuration and initializes it.
      Parameters:
      config - The FtpConfig containing connection details.
    • SshjSftp

      public SshjSftp(net.schmizz.sshj.SSHClient sshClient, Charset charset)
      Constructs an SshjSftp instance with an existing SSHClient and character set, and initializes it.
      Parameters:
      sshClient - The pre-configured SSHClient instance.
      charset - The character set for file names.
  • Method Details

    • of

      public static SshjSftp of(String sshHost, String sshUser, String sshPass)
      Creates an SshjSftp instance 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 SshjSftp instance.
    • of

      public static SshjSftp of(String sshHost, int sshPort, String sshUser, String sshPass)
      Creates an SshjSftp instance 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 SshjSftp instance.
    • of

      public static SshjSftp of(String sshHost, int sshPort, String sshUser, String sshPass, Charset charset)
      Creates an SshjSftp instance 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 SshjSftp instance.
    • 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

      public AbstractFtp reconnectIfTimeout()
      Description copied from interface: Ftp
      Reconnects 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 Ftp instance, allowing for method chaining.
    • cd

      public boolean cd(String directory)
      Description copied from interface: Ftp
      Changes 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:
      true if the directory was successfully changed; false otherwise.
    • pwd

      public String pwd()
      Description copied from interface: Ftp
      Retrieves the current remote directory (working directory) on the FTP server.
      Returns:
      The absolute path of the current working directory as a String.
    • rename

      public boolean rename(String oldPath, String newPath)
      Description copied from interface: Ftp
      Renames 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:
      true if the rename operation was successful; false otherwise.
    • mkdir

      public boolean mkdir(String dir)
      Description copied from interface: Ftp
      Creates a new directory in the current remote working directory.
      Parameters:
      dir - The name of the directory to create.
      Returns:
      true if the directory was created successfully; false otherwise.
    • ls

      public List<String> ls(String path)
      Description copied from interface: Ftp
      Lists all file and directory names within a specified remote directory (non-recursive).
      Parameters:
      path - The path to the directory to list.
      Returns:
      A List of Strings, where each string is the name of a file or directory.
    • delFile

      public boolean delFile(String path)
      Description copied from interface: Ftp
      Deletes a specified file on the remote FTP server.
      Parameters:
      path - The path to the file to delete.
      Returns:
      true if the file was deleted successfully; false otherwise.
    • delDir

      public boolean delDir(String dirPath)
      Description copied from interface: Ftp
      Deletes 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:
      true if the directory and its contents were deleted successfully; false otherwise.
    • uploadFile

      public boolean uploadFile(String destPath, File file)
      Description copied from interface: Ftp
      Uploads a local file to the target server. The destination path on the server can be specified. If destPath is 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 be null (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 local File object to upload.
      Returns:
      true if the upload was successful; false otherwise.
    • download

      public void download(String destPath, File outFile)
      Description copied from interface: Ftp
      Downloads a file from the remote FTP server to a specified local file or directory. If outFile is 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 local File or directory where the downloaded file should be saved.
    • recursiveDownloadFolder

      public void recursiveDownloadFolder(String destPath, File targetDir)
      Description copied from interface: Ftp
      Recursively 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

      public InputStream getFileStream(String path)
      Description copied from interface: Ftp
      Reads a file from the FTP server and provides its content as an InputStream. This allows for streaming processing of remote file content.
      Parameters:
      path - The path to the file on the FTP server.
      Returns:
      An InputStream providing access to the file's content.
    • close

      public void close()
    • containsFile

      public boolean containsFile(String fileDir)
      Checks if the specified file exists on the remote server.
      Parameters:
      fileDir - The absolute path of the file.
      Returns:
      true if the file exists, false otherwise.
    • command

      public String command(String exec)
      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.