无法从Java应用程序连接到远程FileZilla ftp服务器

时间:2013-01-02 13:35:06

标签: java filezilla

我在Windows机器上运行了一个远程FileZilla ftp服务器。 ftp服务器需要显式FTP over TLS 。协议是FTP而不是SFTP。我无法更改此服务器的设置。我可以使用filezilla gui客户端连接到这个服务器。

现在我需要使用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");
    }

但是然后登录= false我得到:“FTP登录失败”。如果我调试apache源,我看到回复代码是:530 =“未登录”:http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes

1 个答案:

答案 0 :(得分: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);