- All Superinterfaces:
AutoCloseable,Closeable
- All Known Implementing Classes:
AbstractFtp,CommonsFtp,JschSftp,SshjSftp
Unified interface for FTP (File Transfer Protocol) operations. This interface defines a set of common file transfer
and management operations that can be performed on a remote FTP server, abstracting away the specifics of different
FTP client implementations.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final CharsetThe default character set used for FTP operations, typically UTF-8. -
Method Summary
Modifier and TypeMethodDescriptionbooleanChanges the current working directory on the remote FTP server to the specified directory.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.booleanChecks if a file or directory exists at the specified path on the remote server.Retrieves the FTP configuration associated with this FTP client instance.getFileStream(String path) Reads a file from the FTP server and provides its content as anInputStream.default 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).booleanCreates a new directory in the current remote working directory.voidCreates the specified folder and any necessary parent directories on the remote server.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 sourceDir, 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.default booleantoParent()Changes the current working directory to the parent directory.booleanuploadFile(String destPath, File file) Uploads a local file to the target server.
-
Field Details
-
DEFAULT_CHARSET
The default character set used for FTP operations, typically UTF-8.
-
-
Method Details
-
getConfig
FtpConfig getConfig()Retrieves the FTP configuration associated with this FTP client instance.- Returns:
- The
FtpConfigobject containing connection and operational settings.
-
reconnectIfTimeout
Ftp reconnectIfTimeout()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
Ftpinstance, allowing for method chaining.
-
pwd
String pwd()Retrieves the current remote directory (working directory) on the FTP server.- Returns:
- The absolute path of the current working directory as a
String.
-
cd
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:
trueif the directory was successfully changed;falseotherwise.
-
toParent
default boolean toParent()Changes the current working directory to the parent directory. This is a convenience method that callscd(String)withSymbol.DOUBLE_DOT.- Returns:
trueif the parent directory was successfully changed;falseotherwise.
-
exist
Checks if a file or directory exists at the specified path on the remote server. Special handling is provided for empty paths, and paths ending with directory separators but not representing actual directories, or special directory names like "." or "..".- Parameters:
path- The path to the file or directory to check.- Returns:
trueif the file or directory exists;falseotherwise.
-
isDir
Determines 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.
-
rename
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:
trueif the rename operation was successful;falseotherwise.
-
mkdir
Creates 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.
-
mkDirs
Creates the specified folder and any necessary parent directories on the remote server. This method ensures that the full path exists. After creation, the working directory is reset to its default or initial state.- Parameters:
dir- The absolute path of the folder to create.
-
ls
Lists all file and directory names within a specified remote directory (non-recursive). -
delFile
Deletes 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
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:
trueif the directory and its contents were deleted successfully;falseotherwise.
-
uploadFile
Uploads 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
Downloads 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:
path- The path to the file on the remote FTP server.outFile- The localFileor directory where the downloaded file should be saved.
-
recursiveDownloadFolder
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:
sourceDir- The source directory on the FTP server to download from.targetDir- The target directory on the local machine where files will be saved.
-
getFileStream
Reads 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.
-