我正在尝试使用 ftp4j lib 从具有 TLS 的 FileZilla FTP 服务器获取文件列表。
import it.sauronsoftware.ftp4j.FTPClient;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.Arrays;
public class FTP {
public static void main(String args[]) throws Exception {
System.setProperty("http.protocols", "TLSv1,TLSv1.1,TLSv1.2");
//tried to avoid closing connection during the handshake
//load and set certificate
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream("mykeystore.jks"), "root12".toCharArray());
FTPClient client = new FTPClient();
SSLContext sslContext = null;
try {
javax.net.ssl.TrustManagerFactory tmf = javax.net.ssl.TrustManagerFactory
.getInstance(javax.net.ssl.KeyManagerFactory
.getDefaultAlgorithm());
tmf.init(keyStore);
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), new SecureRandom());
} catch (Exception e) {
e.printStackTrace();
}
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
client.setSSLSocketFactory(sslSocketFactory);
client.setSecurity(FTPClient.SECURITY_FTPES);
client.setCharset("UTF-8");
client.setPassive(true);
String[] arg = client.connect("localhost", 21);
System.out.println(Arrays.toString(arg));
client.login("admin", "pass"); //OK
client.noop(); // aka Ping is OK
String s = client.currentDirectory(); //OK
client.changeDirectory("/"); //OK
String[] files = client.listNames(); //Exception here
System.out.println(Arrays.toString(files));
client.disconnect(true);
}
}
我有一个例外
线程“main”中的异常 it.sauronsoftware.ftp4j.FTPException [code=450, message= TLS 数据连接会话未恢复或会话与控制连接不匹配] 在 it.sauronsoftware.ftp4j.FTPClient.listNames(FTPClient .java:2407) 在 FTP.main(FTP.java:49)
我尝试使用主动模式,但也无济于事(ping 命令后失败)
线程“main”中的异常 it.sauronsoftware.ftp4j.FTPException [code=421,message= Rejected command,请求的 IP 地址与控制连接 IP 不匹配。]
您能否回答我做错了什么或者 ftp4j 库实际上支持 FTPES 和 TLS 吗?
FileZilla 服务器 TLS 配置的屏幕截图: