java.lang.Object
org.miaixz.bus.extra.ftp.SimpleFtpServer
A simple encapsulation of an FTP server based on Apache FtpServer (http://apache.apache.org/ftpserver-project/). This
class provides convenient methods for configuring and starting an embedded FTP server.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddAnonymous(String homePath) Adds an anonymous user to the FTP server.Adds an FTP action behavior listener (Ftplet) to the server.addUser(org.apache.ftpserver.ftplet.User user) Adds an FTP user to the server's user manager.Deletes an FTP user from the server's user manager.org.apache.ftpserver.listener.ListenerFactoryRetrieves the underlyingListenerFactoryto allow for advanced configuration of FTP server listeners, such as port, SSL, etc.org.apache.ftpserver.FtpServerFactoryRetrieves the underlyingFtpServerFactoryto allow for advanced configuration of the FTP server properties.org.apache.ftpserver.ftplet.UserManagerRetrieves theUserManagerfor managing FTP user accounts.static SimpleFtpServerof()Creates a newSimpleFtpServerinstance.setConnectionConfig(org.apache.ftpserver.ConnectionConfig connectionConfig) Sets the connection configuration for the FTP server.setPort(int port) Sets the default port for the FTP server.Configures the FTP server to use SSL for secure connections with a given keystore file and password.setSsl(org.apache.ftpserver.ssl.SslConfiguration ssl) Configures the FTP server to use SSL for secure connections.setUserManager(org.apache.ftpserver.ftplet.UserManager userManager) Sets a customUserManagerfor the FTP server.setUsersConfig(File propertiesFile) Sets a custom user information configuration file.voidstart()Starts the FTP server.
-
Constructor Details
-
SimpleFtpServer
public SimpleFtpServer()Constructs a newSimpleFtpServerinstance. InitializesFtpServerFactoryandListenerFactory.
-
-
Method Details
-
of
Creates a newSimpleFtpServerinstance. To start the server, callstart().- Returns:
- A new
SimpleFtpServerinstance.
-
getServerFactory
public org.apache.ftpserver.FtpServerFactory getServerFactory()Retrieves the underlyingFtpServerFactoryto allow for advanced configuration of the FTP server properties.- Returns:
- The
FtpServerFactoryinstance.
-
setConnectionConfig
Sets the connection configuration for the FTP server. UseConnectionConfigFactoryto create aConnectionConfigobject.- Parameters:
connectionConfig- TheConnectionConfigobject to set.- Returns:
- This
SimpleFtpServerinstance, allowing for method chaining.
-
getListenerFactory
public org.apache.ftpserver.listener.ListenerFactory getListenerFactory()Retrieves the underlyingListenerFactoryto allow for advanced configuration of FTP server listeners, such as port, SSL, etc.- Returns:
- The
ListenerFactoryinstance.
-
setPort
Sets the default port for the FTP server. If not set, the default port 21 will be used.- Parameters:
port- The port number to set. Must be a valid port number.- Returns:
- This
SimpleFtpServerinstance, allowing for method chaining. - Throws:
IllegalArgumentException- if the provided port is not a valid port number.
-
getUserManager
public org.apache.ftpserver.ftplet.UserManager getUserManager()Retrieves theUserManagerfor managing FTP user accounts. This manager can be used to add, find, and delete user information.- Returns:
- The
UserManagerinstance.
-
setUserManager
Sets a customUserManagerfor the FTP server. This is typically used when user information is configured via a properties file or a custom database.- Parameters:
userManager- The customUserManagerto set.- Returns:
- This
SimpleFtpServerinstance, allowing for method chaining.
-
addUser
Adds an FTP user to the server's user manager.- Parameters:
user- TheUserobject containing FTP user information.- Returns:
- This
SimpleFtpServerinstance, allowing for method chaining. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if anFtpExceptionoccurs during user saving.
-
addAnonymous
Adds an anonymous user to the FTP server. The anonymous user will have read/write permissions to the specified home directory.- Parameters:
homePath- The home directory path for the anonymous user.- Returns:
- This
SimpleFtpServerinstance, allowing for method chaining.
-
delUser
Deletes an FTP user from the server's user manager.- Parameters:
userName- The username of the FTP user to delete.- Returns:
- This
SimpleFtpServerinstance, allowing for method chaining. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if anFtpExceptionoccurs during user deletion.
-
setSsl
Configures the FTP server to use SSL for secure connections. UseSslConfigurationFactoryto create anSslConfigurationobject.- Parameters:
ssl- TheSslConfigurationobject containing SSL/TLS settings.- Returns:
- This
SimpleFtpServerinstance, allowing for method chaining.
-
setSsl
Configures the FTP server to use SSL for secure connections with a given keystore file and password.- Parameters:
keystoreFile- TheFileobject pointing to the keystore file (e.g., JKS, PKCS12).password- The password for the keystore file.- Returns:
- This
SimpleFtpServerinstance, allowing for method chaining.
-
setUsersConfig
Sets a custom user information configuration file. This method will reset the current user manager and configure it to load users from the specified properties file.- Parameters:
propertiesFile- TheFileobject pointing to the user properties file.- Returns:
- This
SimpleFtpServerinstance, allowing for method chaining.
-
addFtplet
Adds an FTP action behavior listener (Ftplet) to the server. By implementingFtplet, custom logic can be executed in response to user actions.- Parameters:
name- The name of the Ftplet.ftplet- TheFtpletinstance, which defines custom listener rules.- Returns:
- This
SimpleFtpServerinstance, allowing for method chaining.
-
start
public void start()Starts the FTP server. This method will block the current thread until the server is shut down. A default listener is created if none is explicitly configured.- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if anFtpExceptionoccurs during server startup.
-