简体   繁体   中英

Creating FTP Client Login Issue In java

I am using commons.net api in order to create connection to the FTP server. simple code like

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 Login as shown below

        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);

but i found trouble in order to login to the server. i can start server easily but couldn't login to the server.. my output look like

connect done

after long time get exception like

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)

I can't move forward from here...

sorry for bothering you but finally i founded a solution.. it was my silly mistake..i was using commons.net-3.0 version when i upgraded that jar to latest version eg commons.net-3.3 the error was gone totally...so if you are getting this error just upgrade your jars just like that..

"have fun codding"..

The code looks fine (and runs fine), so it's probably something in your networking setup. Try changing the "192.168.1.110" address to "localhost" in client and server and see if that works. If it does then you need to investigate your network setup.

这是由于使用 ftp 服务器设置的“被动”模式导致了此问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM