我正在用Java实现一个项目的FTP服务器。我可以启动服务器但是当我尝试与客户端连接时,它会停留在"等待欢迎消息"。我看过几个例子,但我不知道我哪里出错了。这是我的课程。我最终会将其中的一部分打破其他方法。
为了这篇文章的目的,已经清除了用户参数。
public class FTPServer {
final int PORT = 2221;
String userfile = "";
String username="";
String password = ""
String homedir ="";
private FtpServer server=null;
public FTPServer() {}
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);
// replace the default listener
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) {
// TODO Auto-generated catch block
this.StopServer();
e1.printStackTrace();
}
serverFactory.setUserManager(um);
server = serverFactory.createServer();
}
public void StopServer(){ this.server.stop(); }
public void StartServer()
{
try {
server.start();
} catch (FtpException e) {
// handle this eventually, good enough for testing now
e.printStackTrace();
}
}
以下是创建服务器并启动和停止服务器的代码
final int port = 0;
final String ipaddress = "";
FTPServer server = new FTPServer(ipaddress,port);
server.StartServer();
server.StopServer();
答案 0 :(得分:0)
我会说FtpServer.Start
只开始侦听传入端口。它不会阻止。之后,您可以通过调用.Stop
来终止服务器。
您必须明确等待代码才能使服务器继续运行。
server.StartServer();
Thread.sleep(Long.MAX_VALUE);