You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache Mina Java FTP服务器实现:客户端卡在等待欢迎消息

Java FTP Server Stuck on "Waiting for Welcome Message" When Client Connects

I see you're having trouble with your custom Java FTP server—clients get stuck waiting for the welcome message even though the server seems to start fine. Let's break down the issues in your code and fix them step by step:

1. Misplaced Listener Configuration Logic

Looking at your FTPServer constructor, the code to add the listener and set connection config is trapped inside the else block for port validation. That means when you pass port = 0 (which you do in your startup code), this critical setup never runs—your server doesn't actually listen for incoming connections at all!

Fix:

Move the listener and connection config code outside the else block so it runs regardless of the port value:

public FTPServer(final String ipaddress, final int port){
    FtpServerFactory serverFactory = new FtpServerFactory();
    ListenerFactory listenerfactory = new ListenerFactory();
    listenerfactory.setDataConnectionConfiguration(
        new DataConnectionConfigurationFactory().createDataConnectionConfiguration());
    ConnectionConfigFactory connection = new ConnectionConfigFactory();
    connection.setMaxLoginFailures(10);
    connection.setLoginFailureDelay(5);
    connection.setAnonymousLoginEnabled(false);

    // set the ip address of the listener
    listenerfactory.setServerAddress(ipaddress);
    // set the port of the listener
    if (port == 0) {
        listenerfactory.setPort(PORT);
    } else {
        listenerfactory.setPort(port);
    }
    // Move these lines OUTSIDE the else block
    serverFactory.addListener("default", listenerfactory.createListener());
    serverFactory.setConnectionConfig(connection.createConnectionConfig());

    PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
    userManagerFactory.setFile(new File(userfile));
    userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());
    UserManager um = userManagerFactory.createUserManager();
    BaseUser user = new BaseUser();
    user.setName(username);
    user.setPassword(password);
    user.setHomeDirectory(homedir);
    try {
        um.save(user);
    } catch (FtpException e1) {
        this.StopServer();
        e1.printStackTrace();
    }
    serverFactory.setUserManager(um);
    server = serverFactory.createServer();
}

2. Server Gets Stopped Immediately After Startup

In your startup code, you call server.StartServer() and then immediately server.StopServer()—the server has no time to accept client connections before it's shut down. That's definitely going to cause clients to hang mid-connection attempt.

Fix:

Keep the server running until you intentionally stop it. For testing, you can wait for user input or add a delay:

final int port = 0;
final String ipaddress = "127.0.0.1"; // Use a valid IP, e.g., localhost
FTPServer server = new FTPServer(ipaddress, port);
server.StartServer();

// Keep the server running (example: wait for user input to stop)
try {
    System.out.println("Server running... Press Enter to stop.");
    System.in.read();
} catch (IOException e) {
    e.printStackTrace();
}

server.StopServer();

3. Uninitialized Member Variables

Your userfile, username, password, and homedir variables are empty strings. This will break user authentication and home directory setup, which can also disrupt the connection flow.

Fix:

Initialize these variables with valid values before using them:

// Example initialization (replace with your actual values)
String userfile = "./ftp-users.properties";
String username = "your-ftp-user";
String password = "your-ftp-password";
String homedir = "/path/to/your/ftp/home";

Additional Notes

  • Ensure the homedir path exists on your system and the server has read/write permissions for it.
  • Verify no other process is using the port you're targeting (2221 by default).
  • Add proper error handling for file operations (like checking if userfile is writable) to avoid silent failures.

Once you apply these fixes, your FTP server should properly accept connections and send the welcome message to clients.

内容的提问来源于stack exchange,提问作者crokett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:08:55