Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从java应用程序连接到远程FileZilla ftp服务器_Java_Filezilla - Fatal编程技术网

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

无法从java应用程序连接到远程FileZilla ftp服务器,java,filezilla,Java,Filezilla,我有一个运行在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 {

我有一个运行在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);
关于如何使用org.apache.commons.net从java代码连接到Filezilla服务器,有什么想法吗

编辑: 我现在已尝试更改为FTPClient(即使这允许我设置显式TLS):


但是login=false,我得到:“FTP登录失败”。如果我调试apache源代码,我会看到回复代码是:530=“未登录”:

创建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);

看起来像是安全问题。您是否尝试了FTPClient:而不是FTPSClient?您确定FTP服务器的证书有效吗?
  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);