2013-01-02 209 views
1

我有一臺運行在Windows機器上的遠程FileZilla ftp服務器。 ftp服務器需要通過TLS的明確FTP。協議是FTP和NOT SFTP。我無法更改此服務器的設置。我可以使用filezilla gui客戶端連接到此服務器。無法從java應用程序連接到遠程FileZilla ftp服務器

現在我需要使用org.apache.commons.net連接到通過Java應用程序的FileZilla服務器:

private void connect(String host, String user, String password) { 
    try { 
     FTPSClient ftpClient = new FTPSClient(false); 
     ftpClient.connect(host); 
     int reply = ftpClient.getReplyCode(); 
     if (FTPReply.isPositiveCompletion(reply)) { 
     // Login 
     if (ftpClient.login(user, password)) { 

      // Set protection buffer size 
      ftpClient.execPBSZ(0); 
      // Set data channel protection to private 
      ftpClient.execPROT("P"); 
      // Enter local passive mode 
      ftpClient.enterLocalPassiveMode(); 
      ftpClient.logout(); 
     } else { 
      System.out.println("FTP login failed"); 
     } 
     // Disconnect 
     ftpClient.disconnect(); 
     } else { 
     System.out.println("FTP connect to host failed"); 
     } 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
     System.out.println("FTP client received network error"); 
    } 
    } 

但是當我運行上面的代碼我得到:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateExpiredException: NotAfter: Thu Aug 30 13:31:23 CEST 2012 
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1764) 

當涉及到:

ftpClient.connect(host); 

任何想法如何從Java代碼連接到一個FileZilla服務器使用例如。 org.apache.commons.net?

編輯: 我現在試圖改變FTPClient(儘管這確實讓我設置明確TLS):

FTPClient ftpClient = new FTPClient(); 
    // Connect to host 
    ftpClient.connect(host); 
    int reply = ftpClient.getReplyCode(); 
    if (FTPReply.isPositiveCompletion(reply)) { 

    // Login 
    boolean login = ftpClient.login(user, password); 
    if (login) { 
     ftpClient.enterLocalPassiveMode(); 
     ftpClient.logout(); 
    } else { 
     System.out.println("FTP login failed"); 
    } 

但隨後登錄=假,我得到:「FTP登錄失敗」。如果我調試Apache源代碼我看到的回覆代碼爲:530 =「未登錄」:http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes

+0

看起來像一個安全問題。您是否嘗試過FTPClient:http://commons.apache.org/net/api-3.2/org/apache/commons/net/ftp/FTPClient.html而不是FTPSClient? – mcalex

+0

是的,請參閱編輯後 – u123

+0

您確定FTP服務器的證書有效嗎? – cpast

回答

1

創建的SSLContext解決了這個問題:

SSLContext sslContext = SSLContext.getInstance("TLS"); 
    TrustManager tm = new X509TrustManager() { 
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
    } 

    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
    } 

    public X509Certificate[] getAcceptedIssuers() { 
     return null; 
    } 
    }; 
    sslContext.init(null, new TrustManager[] { tm }, null); 
    FTPSClient ftpsClient = new FTPSClient(sslContext);