2012-06-06 3 views
2

Мне интересно, правильно ли я подключаюсь и отключаюсь от 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; 
} 
} 

ответ

1

Если мы посмотрим на documentation он показывает с помощью 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); 
    } 

вернуться Также неверно, если закрытие не удается

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

Возможно, вы захотите проверить проект FtpClient из проекта apache commons: FtpClient. В javaDoc содержатся некоторые хорошо разработанные примеры.

Смежные вопросы