gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:33:12 26 4
gpt4 key购买 nike

我有一个在 Windows 计算机上运行的远程 FileZilla ftp 服务器。 ftp 服务器需要基于 TLS 的显式 FTP。该协议(protocol)是 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

最佳答案

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

关于java - 无法从 java 应用程序连接到远程 FileZilla ftp 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14126487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com