Module bus.extra

Class AbstractFtp

java.lang.Object
org.miaixz.bus.extra.ftp.AbstractFtp
All Implemented Interfaces:
Closeable, AutoCloseable, Ftp
Direct Known Subclasses:
CommonsFtp, JschSftp, SshjSftp

public abstract class AbstractFtp extends Object implements Ftp
Abstract base class for FTP operations. This class provides common utility methods and a basic structure for FTP client implementations, handling tasks like path existence checks, directory creation, and robust file downloading.
Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • ftpConfig

      protected FtpConfig ftpConfig
      The FTP configuration settings used by this client.
  • Constructor Details

    • AbstractFtp

      protected AbstractFtp(FtpConfig config)
      Constructs an AbstractFtp instance with the specified FTP configuration.
      Parameters:
      config - The FtpConfig containing connection and operational settings.
  • Method Details

    • getConfig

      public FtpConfig getConfig()
      Retrieves the FTP configuration associated with this FTP client instance.
      Specified by:
      getConfig in interface Ftp
      Returns:
      The FtpConfig object containing connection and operational settings.
    • exist

      public boolean exist(String path)
      Checks if a file or directory exists at the specified path on the remote server. This method handles both directory and file existence checks, including special cases for empty paths, paths ending with file separators, and special directory names like "." and "..".
      Specified by:
      exist in interface Ftp
      Parameters:
      path - The path to the file or directory to check.
      Returns:
      true if the file or directory exists; false otherwise.
    • mkDirs

      public void mkDirs(String dir)
      Creates the specified folder and its parent directories on the remote server. This method navigates through the path components, creating directories as needed. After creation, the working directory is restored to its original state.
      Specified by:
      mkDirs in interface Ftp
      Parameters:
      dir - The absolute path of the folder to create.
    • download

      public void download(String path, File outFile, String tempFileSuffix)
      Downloads a file from the remote server to a local file, ensuring atomicity by using a temporary file. The principle of this method is to first download the file to a temporary file in the same directory as the target file. Once the download is complete, the temporary file is renamed to the final target name. This prevents incomplete files from being left behind in case of download errors.
      Parameters:
      path - The file path on the remote server.
      outFile - The local File or directory where the downloaded file should be saved. If it's a directory, the server's filename will be used.
      tempFileSuffix - The suffix for temporary files, e.g., ".temp". If blank, defaults to ".temp".
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an error occurs during download or file operations (e.g., I/O errors, renaming issues).