【问题标题】:Unable to connect to remote FileZilla ftp server from java application无法从 Java 应用程序连接到远程 FileZilla ftp 服务器
【发布时间】:2013-01-02 13:35:06
【问题描述】:

我有一个在 Windows 机器上运行的远程 FileZilla ftp 服务器。 ftp 服务器需要基于 TLS 的显式 FTP。协议是 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 = "Not logged in" : http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes

【问题讨论】:

标签: java filezilla


【解决方案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);

【讨论】:

    猜你喜欢
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    相关资源
    最近更新 更多