Module bus.extra

Class SimpleFtpServer

java.lang.Object
org.miaixz.bus.extra.ftp.SimpleFtpServer

public class SimpleFtpServer extends Object
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 Details

    • SimpleFtpServer

      public SimpleFtpServer()
      Constructs a new SimpleFtpServer instance. Initializes FtpServerFactory and ListenerFactory.
  • Method Details

    • of

      public static SimpleFtpServer of()
      Creates a new SimpleFtpServer instance. To start the server, call start().
      Returns:
      A new SimpleFtpServer instance.
    • getServerFactory

      public org.apache.ftpserver.FtpServerFactory getServerFactory()
      Retrieves the underlying FtpServerFactory to allow for advanced configuration of the FTP server properties.
      Returns:
      The FtpServerFactory instance.
    • setConnectionConfig

      public SimpleFtpServer setConnectionConfig(org.apache.ftpserver.ConnectionConfig connectionConfig)
      Sets the connection configuration for the FTP server. Use ConnectionConfigFactory to create a ConnectionConfig object.
      Parameters:
      connectionConfig - The ConnectionConfig object to set.
      Returns:
      This SimpleFtpServer instance, allowing for method chaining.
    • getListenerFactory

      public org.apache.ftpserver.listener.ListenerFactory getListenerFactory()
      Retrieves the underlying ListenerFactory to allow for advanced configuration of FTP server listeners, such as port, SSL, etc.
      Returns:
      The ListenerFactory instance.
    • setPort

      public SimpleFtpServer setPort(int port)
      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 SimpleFtpServer instance, 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 the UserManager for managing FTP user accounts. This manager can be used to add, find, and delete user information.
      Returns:
      The UserManager instance.
    • setUserManager

      public SimpleFtpServer setUserManager(org.apache.ftpserver.ftplet.UserManager userManager)
      Sets a custom UserManager for the FTP server. This is typically used when user information is configured via a properties file or a custom database.
      Parameters:
      userManager - The custom UserManager to set.
      Returns:
      This SimpleFtpServer instance, allowing for method chaining.
    • addUser

      public SimpleFtpServer addUser(org.apache.ftpserver.ftplet.User user)
      Adds an FTP user to the server's user manager.
      Parameters:
      user - The User object containing FTP user information.
      Returns:
      This SimpleFtpServer instance, allowing for method chaining.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an FtpException occurs during user saving.
    • addAnonymous

      public SimpleFtpServer addAnonymous(String homePath)
      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 SimpleFtpServer instance, allowing for method chaining.
    • delUser

      public SimpleFtpServer delUser(String userName)
      Deletes an FTP user from the server's user manager.
      Parameters:
      userName - The username of the FTP user to delete.
      Returns:
      This SimpleFtpServer instance, allowing for method chaining.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an FtpException occurs during user deletion.
    • setSsl

      public SimpleFtpServer setSsl(org.apache.ftpserver.ssl.SslConfiguration ssl)
      Configures the FTP server to use SSL for secure connections. Use SslConfigurationFactory to create an SslConfiguration object.
      Parameters:
      ssl - The SslConfiguration object containing SSL/TLS settings.
      Returns:
      This SimpleFtpServer instance, allowing for method chaining.
    • setSsl

      public SimpleFtpServer setSsl(File keystoreFile, String password)
      Configures the FTP server to use SSL for secure connections with a given keystore file and password.
      Parameters:
      keystoreFile - The File object pointing to the keystore file (e.g., JKS, PKCS12).
      password - The password for the keystore file.
      Returns:
      This SimpleFtpServer instance, allowing for method chaining.
    • setUsersConfig

      public SimpleFtpServer setUsersConfig(File propertiesFile)
      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 - The File object pointing to the user properties file.
      Returns:
      This SimpleFtpServer instance, allowing for method chaining.
    • addFtplet

      public SimpleFtpServer addFtplet(String name, org.apache.ftpserver.ftplet.Ftplet ftplet)
      Adds an FTP action behavior listener (Ftplet) to the server. By implementing Ftplet, custom logic can be executed in response to user actions.
      Parameters:
      name - The name of the Ftplet.
      ftplet - The Ftplet instance, which defines custom listener rules.
      Returns:
      This SimpleFtpServer instance, 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 an FtpException occurs during server startup.