2013-10-14 3 views
1

http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.htmlJava Apache FTPClient API : finally 절에서 logout()이 왜 안되나요?

내가 마지막 절에서 예를 연결 해제()을 발견,하지만 우리가 로그 아웃 할 필요가 없습니다 이유를 아는 사람

FTPClient ftp = new FTPClient(); 
FTPClientConfig config = new FTPClientConfig(); 
config.setXXX(YYY); // change required options 
ftp.configure(config); 
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); 
} 

로그 아웃()에 대해 동일한 작업을 수행하지 않습니다() 할 때 우리가 예외를 잡았나요?

+0

를하는 것이 바람직하기 때문에 또한 disconnect() 함수는 또한 예외가 발생하지만,이 logout() 기능하지 않는 input, output, socket 해제 자원을 닫는다. :) – Sage

답변

0

내가 FTPClient 라이브러리에 대해 잘 몰라요하지만 난 그게 프로세스의 일환으로 로그 아웃 것을 의미 서버에서 분리 가정하는 것이 안전하다고 생각하는 경우 워드 프로세서에 주어진 적용, 고려 설명 :

해제() : FTP 서버에 대한 연결을 닫고 연결 매개 변수를 기본값으로 복원합니다.

logout() : QUIT 명령을 전송하여 FTP 서버에서 로그 아웃합니다. 우리는 예외를 잡을 때 우리가) (로그 아웃 할 필요가 없습니다 왜

http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html

2

누구나 알아? 다음

ftp.logout() 함수의 내부 코드이다

public boolean logout() throws IOException 
{ 
     return FTPReply.isPositiveCompletion(quit()); 
} 

quit() 함수 FTP ServersendCommand(FTPCommand.QUIT)를 사용하여 명령을 송신한다. 연결 예외가 발생하면 FTP Server으로 연결할 수 없습니다. logout()을 호출하면 FTP 서버에 다시 쓰고 추가 던지는 예외로 리소스를 만들려고 시도합니다. 이 disconnect() 함수의 다음의 소스 코드에서 알 수있는 바와 같이 :

public void disconnect() throws IOException 
{ 
     if (_socket_ != null) _socket_.close(); 
     if (_input_ != null) _input_.close(); 
     if (_output_ != null) _output_.close(); 
     if (_socket_ != null) _socket_ = null; 
     _input_ = null; 
     _output_ = null; 
     _controlInput_ = null; 
     _controlOutput_ = null; 
     _newReplyString = false; 
     _replyString = null; 
} 
+0

어쨌든'disconnect'를 호출하려는 경우 실제로'logout'을 호출 할 이유가 있습니까? 아니면'logout'은 다른 사용자로 곧바로 로그를 남기고 싶은 (매우 드문 경우) 상황에서만 유용할까요? – mrec