java 正确检查FTP服务器连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13836989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Properly check FTP server connection
提问by Michael A
I open a connection to an FTP server at the start of my program.
我在程序开始时打开了一个到 FTP 服务器的连接。
Before I perform operations on the server I want to check if the connection was successfully established. The easiest fast manner so if the connection is gone, I will try to connect again.
在我对服务器执行操作之前,我想检查连接是否成功建立。最简单的快速方式,因此如果连接消失,我将尝试再次连接。
I used this code to do this:
我用这个代码来做到这一点:
private boolean checkConnection()
{
try
{
boolean success = ftpClient.login(user_name, password);
if(success)
return true;
else
return false;
}
}
But this method throws a NullPointer exception when the connection was closed.
但是这个方法在连接关闭时会抛出一个 NullPointer 异常。
I can check the connection with the ftpClient.connect(server, port);
but that is like attampting to connect all over again.
我可以检查与 的连接,ftpClient.connect(server, port);
但这就像尝试重新连接。
What is the bes way to check the connection?
检查连接的最佳方法是什么?
回答by Michael A
Trying to send a simple sendNoOp()
and checking the reply might be a good way to lightly check the connecion:
尝试发送一个简单的sendNoOp()
并检查回复可能是轻松检查连接的好方法:
private boolean checkConnectionWithOneRetry()
{
try
{
// Sends a NOOP command to the FTP server.
boolean answer = ftpClient.sendNoOp();
if(answer)
return true;
else
{
System.out.println("Server connection failed!");
boolean success = reconnect();
if(success)
{
System.out.println("Reconnect attampt have succeeded!");
return true;
}
else
{
System.out.println("Reconnect attampt failed!");
return false;
}
}
}
catch (FTPConnectionClosedException e)
{
System.out.println("Server connection is closed!");
boolean recon = reconnect();
if(recon)
{
System.out.println("Reconnect attampt have succeeded!");
return true;
}
else
{
System.out.println("Reconnect attampt have failed!");
return false;
}
}
catch (IOException e)
{
System.out.println("Server connection failed!");
boolean recon = reconnect();
if(recon)
{
System.out.println("Reconnect attampt have succeeded!");
return true;
}
else
{
System.out.println("Reconnect attampt have failed!");
return false;
}
}
catch (NullPointerException e)
{
System.out.println("Server connection is closed!");
boolean recon = reconnect();
if(recon)
{
System.out.println("Reconnect attampt have succeeded!");
return true;
}
else
{
System.out.println("Reconnect attampt have failed!");
return false;
}
}
}
回答by Mohammod Hossain
private FTPClient ftp = null;
private void connect()
{
ftp = new FTPClient();
try {
ftp.connect("Server",port);
boolean login = ftp.login("username", "password");
System.out.println(" login "+ login );
} catch (FTPConnectionClosedException e) {
System.err.println("ERROR :: FTP Server Unreachable");
sleep();
connect();
} catch (SocketException e) {
System.err.println("ERROR :: FTP Server Unreachable");
sleep();
connect();
} catch (IOException e) {
System.err.println("ERROR :: FTP Server Unreachable");
sleep();
connect();
}
}
public void sleep(){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}