正确检查FTP服务器连接 [英] Properly check FTP server connection

查看:159
本文介绍了正确检查FTP服务器连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我对服务器执行操作之前,我想检查连接是否成功建立。最简单的快速方式,所以如果连接没有了,我会尝试再次连接。



我使用此代码来执行此操作:

  private boolean checkConnection()
{
try
{
布尔成功= ftpClient.login(user_name,密码) ;
if(成功)
返回true;
else
返回false;


$ / code>

但是这种方法在连接时会抛出一个NullPointer异常关闭。



我可以用 ftpClient.connect(server,port); 来检查连接,但这就像再次重新连接。



检查连接的方式是什么?

解决方案试着发送简单的 sendNoOp()并检查回复可能是轻松检查连接的好方法:

  private boolean checkConnectionWithOneRetry()
{
try
{
//向FTP发送一个NOOP命令服务器。
布尔答案= ftpClient.sendNoOp();
if(answer)
return true;
else
{
System.out.println(服务器连接失败!);
布尔成功= reconnect();
if(成功)
{
System.out.println(重新连接attampt已成功!);
返回true;
}
else
{
System.out.println(重新连接attampt失败!);
返回false;


$ b catch(FTPConnectionClosedException e)
{
System.out.println(服务器连接已关闭!);
boolean recon = reconnect();
if(recon)
{
System.out.println(Ret attampt have succeeded!);
返回true;
}
else
{
System.out.println(重新连接attampt失败!);
返回false;


$ b catch(IOException e)
{
System.out.println(服务器连接失败!);
boolean recon = reconnect();
if(recon)
{
System.out.println(Ret attampt have succeeded!);
返回true;
}
else
{
System.out.println(重新连接attampt失败!);
返回false;


catch(NullPointerException e)
{
System.out.println(服务器连接已关闭!);
boolean recon = reconnect();
if(recon)
{
System.out.println(Ret attampt have succeeded!);
返回true;
}
else
{
System.out.println(重新连接attampt失败!);
返回false;
}
}
}


I open a connection to an FTP server at the start of my program.

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.

I can check the connection with the ftpClient.connect(server, port); but that is like attampting to connect all over again.

What is the bes way to check the connection?

解决方案

Trying to send a simple sendNoOp() and checking the reply might be a good way to lightly check the connecion:

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

这篇关于正确检查FTP服务器连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆