How to connect and disconnect to/from FTP server correctly?

itro picture itro · Jun 6, 2012 · Viewed 9k times · Source

I'm wondering if the way that I connect and disconnect to/from a FTP server is correct or if it can be better.

I'm using sun.net.ftp.FtpClient.

import sun.net.ftp.FtpClient;

public class FTPUtility
{
    public static FtpClient connect(FTPConfig ftpConfig,WebTextArea statusTextArea)
    {
        String hostname = ftpConfig.getFtpServer();
        String username = ftpConfig.getUsername();
        String password = ftpConfig.getPassword();
        String portnumb = ftpConfig.getPort();

            try
            {
                FtpClient client = new FtpClient(hostname);
                statusTextArea.append("Connecting to " + hostname + " as " + username + " on port:" + portnumb );
                client.login(username, password);
                client.binary();
                statusTextArea.append("Connected to " + hostname + " as " + username  + " on port:" + portnumb );
                return client;
            }
            catch (Exception e)
            {
                statusTextArea.append("Failed to connect to " + hostname + " as " + username + "\n".concat(e.getMessage()) );
                return null;
            }

    }

public static boolean disConnect(FtpClient client, WebTextArea statusTextArea)
{     
    boolean success = false;
    if (client != null)
    {
        try
        {
            statusTextArea.append("Disconnecting from server...");
            client.closeServer();
            statusTextArea.append("Disconnected from server." );
            success = true;
        }
        catch (Exception e)
        {
            statusTextArea.append("Failed to disconnect from server. " + "\n".concat(e.getMessage()));
        }
    }

  return success;
}
}

Answer

owen gerig picture owen gerig · Jun 6, 2012

If we look at the documentation it shows using logout() and disconnect() I would also suggestion a better naming convention for your method name disConnect which should just be disconnect(FtpClient client, WebTextArea statusTextArea) (no capital C)

boolean error = false;
    try {
      int reply;
      ftp.connect("ftp.foobar.com");
      System.out.println("Connected to " + server + ".");
      System.out.print(ftp.getReplyString());

      // After connection attempt, you should check the reply code to verify
      // success.
      reply = ftp.getReplyCode();

      if(!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        System.err.println("FTP server refused connection.");
        System.exit(1);
      }
      ... // transfer files
      ftp.logout();
    } catch(IOException e) {
      error = true;
      e.printStackTrace();
    } finally {
      if(ftp.isConnected()) {
        try {
          ftp.disconnect();
        } catch(IOException ioe) {
          // do nothing
        }
      }
      System.exit(error ? 1 : 0);
    }

Also return false if the closing fails

catch (Exception e)
    {
        statusTextArea.append("Failed to disconnect from server. " + "\n".concat(e.getMessage()));
return false;
    }
}