尝试发送一个简单的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)
{