Как да се свързвам и прекъсвам връзката към/от FTP сървър правилно?

Чудя се дали начинът, по който се свързвам и прекъсвам връзката с/от FTP сървър, е правилен или може да бъде по-добър.

Използвам 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;
}
}

person itro    schedule 06.06.2012    source източник


Отговори (2)


Ако разгледаме документацията показва използването на logout() и disconnect() Бих предложил също така по-добра конвенция за именуване за името на вашия метод disConnect, което трябва просто да бъде прекъснато (FtpClient клиент, WebTextArea statusTextArea) (без главно 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);
    }

Също така връща false, ако затварянето е неуспешно

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

Може да искате да разгледате проекта FtpClient от apache commons: FtpClient. JavaDoc съдържа някои добре разработени примери.

person tom    schedule 06.06.2012