我正在使用 commons.net api 来创建到 FTP 服务器的连接。简单的代码如
String Path = "D:\\FTP";
File ftpDirectory = new File(Path);
ftpDirectory.mkdirs();
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
factory.setPort(2221);
factory.setServerAddress("192.168.1.110");
serverFactory.addListener("default", factory.createListener());
PropertiesUserManagerFactory userFactory = new PropertiesUserManagerFactory();
File userFile = new File("D:\\FTP\\users.properties");
userFactory.setFile(userFile);
//UserManager um = userFactory.createUserManager();
UserManager um =new PropertiesUserManager(new ClearTextPasswordEncryptor(),userFile,"admin");
BaseUser user = new BaseUser();
user.setName("test");
user.setPassword("test");
user.setHomeDirectory(Path);
um.save(user);
serverFactory.setUserManager(um);
FtpServer ftpServer = serverFactory.createServer();
ftpServer.start();
FTPClient
如下图登录
FTPClient ftp = new FTPClient();
ftp.connect("192.168.1.110",2221);
System.out.println("connect done");
String loging_success = ftp.login("test", "test") == true ? "success" : "failed";
System.out.println("login: "+ loging_success);
但是我发现无法登录服务器。 我可以轻松启动服务器但无法登录到服务器.. 我的输出看起来像
connect done
长时间后出现异常
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication. at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:313) at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290) at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:474) at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:547) at org.apache.commons.net.ftp.FTP.user(FTP.java:693) at org.apache.commons.net.ftp.FTPClient.login(FTPClient.java:872) at TestFTPServer.connectServer(TestFTPServer.java:213) at TestFTPServer.main(TestFTPServer.java:52)
我不能从这里前进......
最佳答案
很抱歉打扰你,但我终于找到了一个解决方案.. 这是我的愚蠢错误..当我将该 jar 升级到最新版本时,我正在使用 commons.net-3.0 版本,例如commons.net-3.3 错误完全消失了...所以如果您遇到此错误,只需像那样升级您的 jars..
“玩得开心”..
关于java - 在 java 中创建 FTP 客户端登录问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27721037/