1

我有一个在 Windows 机器上运行的远程 FileZilla ftp 服务器。ftp 服务器需要Explicit 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 (即使这确实允许我设置Explicit 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");
    }

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

4

1 回答 1

2

创建 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);
于 2013-01-02T15:30:41.547 回答