我需要使用Java編寫代碼,該代碼使用基於SSL的FTP協議連接到服務器列表,並從中獲取一些文件。每個服務器都有登錄名和密碼列表。我需要使用旋轉中的每個登錄名連接到服務器。事情是這樣的:FTPSClient在註銷後嘗試登錄()時引發異常()
// This is not a real code it's just a sketch of what I need to do.
for (Server server : servers) {
server.connect();
for (LoginPassword loginPassword : server.loginPasswordList) {
server.login(loginPassword);
server.getSomeFiles();
server.logout();
}
server.disconnect();
}
我使用FTPSClient從Apache的百科全書:
ftpClient = ftpClient.connect("host", port);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
if (ftpClient.login("login", "password")) {
ftpClient.execPBSZ(0);
ftpClient.execPROT("P");
ftpClient.enterLocalPassiveMode();
System.out.println(ftpClient.logout()); // true
}
if (ftpClient.login("another_login", "another_password")) { // this line raise an exception
ftpClient.execPBSZ(0);
ftpClient.execPROT("P");
ftpClient.enterLocalPassiveMode();
System.out.println(ftpClient.logout()); // true
}
}
當我試圖註銷並重新登錄一次,相同的連接此代碼失敗。我收到此異常:
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
但是如果我關閉連接(ftpClient.disconnect();)註銷後,然後在代碼中再次打開它(ftpClient.connect(「主機」,口))像下面則它工作正常。
ftpClient = ftpClient.connect("host", port);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
if (ftpClient.login("login", "password")) {
ftpClient.execPBSZ(0);
ftpClient.execPROT("P");
ftpClient.enterLocalPassiveMode();
System.out.println(ftpClient.logout()); // true
}
}
ftpClien.disconnect();
ftpClient = ftpClient.connect("host", port);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
if (ftpClient.login("another_login", "another_password")) {// this works fine
ftpClient.execPBSZ(0);
ftpClient.execPROT("P");
ftpClient.enterLocalPassiveMode();
System.out.println(ftpClient.logout()); // true
}
}
當然,我可以使用此代碼,但問題是,有服務器和登錄的大名單,和新的連接的創建需要〜1秒,但ftpClient.login()只〜50毫秒。因此,如果我將每個登錄使用新連接的代碼,那麼三次登錄到一臺服務器需要大約3150毫秒,而不是1150毫秒。 那麼,註銷後我無法登錄服務器的原因是什麼?
login()沒有註銷()之前工作正常。謝謝! – 2014-10-16 07:44:19