2014-02-18 7 views
0

에서 FTP 서버에 연결할 수 없습니다 내가 얻을 :내가하지만 내 코드가 좋은 것 같다하더라도 내가 어떤 서버에 연결할 수 없습니다 아파치 코 몬즈 인터넷을 사용하여 자바에서 FTP 클라이언트를 개발하기 위해 노력하고있어 자바

java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine 
    at java.net.DualStackPlainSocketImpl.connect0(Native Method) 
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source) 
    at java.net.PlainSocketImpl.connect(Unknown Source) 
    at java.net.SocksSocketImpl.connect(Unknown Source) 
    at java.net.Socket.connect(Unknown Source) 
    at java.net.Socket.connect(Unknown Source) 
    at java.net.Socket.<init>(Unknown Source) 
    at java.net.Socket.<init>(Unknown Source) 
    at org.apache.commons.net.ftp.FTPHTTPClient.connect(FTPHTTPClient.java:131) 
    at org.apache.commons.net.SocketClient.connect(SocketClient.java:296) 
    at awax.meteosat.test.FTPClientTest.connect(FTPClientTest.java:52) 
    at awax.meteosat.test.FTPClientTest.main(FTPClientTest.java:146) 
client disconnected 
Exception in thread "main" java.lang.IllegalStateException: Client disconnected 
    at awax.meteosat.test.FTPClientTest.login(FTPClientTest.java:100) 
    at awax.meteosat.test.FTPClientTest.main(FTPClientTest.java:147) 

이 서버에 연결하려고 시도했습니다 : ftp://ics.ftptest.org/, 완벽하게 작동합니다. 방화벽 연결을 끊으려고하면 같은 결과가 나타납니다.

package awax.meteosat.test; 

import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.SocketException; 
import java.net.URL; 

import org.apache.commons.net.ftp.FTPFile; 
import org.apache.commons.net.ftp.FTPHTTPClient; 
import org.apache.commons.net.ftp.FTPReply; 

public class FTPClientTest { 

    private final String host; 
    private final int port; 
    private final String username; 
    private final String password; 

    private org.apache.commons.net.ftp.FTPClient client; 

    /** 
    * Instanciate FTP server. 
    * 
    * @param host 
    *   FTP server address. 
    * @param username 
    *   FTP username. 
    * @param password 
    *   FTP password. 
    */ 
    public FTPClientTest (final String address, final String username, final String password) { 
     URL url = null; 
     try { 
      url = new URL(address); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     this.host = url != null ? url.getHost() : null; 
     this.port = 0; 
     this.username = username; 
     this.password = password; 
    } 

    public void connect() { 
     if (this.client == null) { 
      this.client = new FTPHTTPClient(this.host, this.port, this.username, this.password); 
      try { 
       // Connecting client 
       if (this.port > 0) { 
        this.client.connect(this.host, this.port); 
       } else { 
        this.client.connect(this.host); 
       } 

       // Check connection 
       int reply = this.client.getReplyCode(); 
       if (!FTPReply.isPositiveCompletion(reply)) { 
        disconnect(); 
       } else { 
        setFtpOptions(); 
       } 
      } catch (SocketException e) { 
       e.printStackTrace(); 
       disconnect(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       disconnect(); 
      } 
     } else { 
      System.err.println("client is null"); 
     } 
    } 

    public void disconnect() { 
     if (this.client != null) { 
      if (this.client.isConnected()) { 
       try { 
        this.client.disconnect(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } else { 
       System.err.println("client disconnected"); 
      } 
     } else { 
      System.err.println("client is null"); 
     } 
    } 

    public boolean login() { 
     if (isConnected()) { 
      try { 
       return this.client.login(this.username, this.password); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       logout(); 
       return false; 
      } 
     } else { 
      throw new IllegalStateException("Client disconnected"); 
     } 
    } 

    public boolean logout() { 
     if (isConnected()) { 
      try { 
       return this.client.logout(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return false; 
      } 
     } else { 
      throw new IllegalStateException("Client disconnected"); 
     } 
    } 

    public FTPFile[] listDirectories() { 
     if (isConnected()) { 
      try { 
       return this.client.listDirectories(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } else { 
      throw new IllegalStateException("Client disconnected"); 
     } 
    } 

    public boolean isConnected() { 
     if (this.client != null) { 
      return this.client.isConnected(); 
     } else { 
      return false; 
     } 
    } 

    private void setFtpOptions() { 
     if (isConnected()) { 
      this.client.enterLocalPassiveMode(); 
     } 
    } 

    public static void main (String args[]) { 
     FTPClientTest ftp = new FTPClientTest("ftp://ics.ftptest.org/", "", ""); 
     ftp.connect(); 
     ftp.login(); 
     for (FTPFile f : ftp.listDirectories()) { 
      System.err.println(f); 
     } 
     ftp.logout(); 
     ftp.disconnect(); 
    } 
} 

답변

2

가 (에 따라이 documentation은 FTP 연결의 실험의 HTTP 프록시입니다입니다) 대신 FTPHTTPClient의 FTPClient를 사용해보십시오 ... : 여기

내 코드입니다, 나는 너희들 하하에 대한 명백한 수 있기를 바랍니다 난 그냥 아파치 웹 사이트에서 FTPClientExample.java 파일을 읽기) 그것은 나에게하지 않았다;

public void connect() { 
    if (this.client == null) { 
     this.client = new FTPClient(); 

, 당신 말이 맞아 잘

+0

아야 작동합니다 그것은 실험적이었습니다. 고맙습니다. – AwaX