Java 使用FTP工具 425 Cannot prepare for data connection.
背景
最近搞了一台新的服务器,服务器存在两个IP,一个公网Ip,一个内网Ip,采用 FileZilla 搭建FTP服务,服务搭建好后,经测试,防火墙,服务模式均没问题,可以连接至FTP服务,但获取目录下文件列表时报错连接超时。
定位异常
public static FTPFile[] getFTPDirectoryFiles(FTPClient ftpClient, String path) {
FTPFile[] files = null;
try {
//让客户端告诉服务端开通一个端口用来数据传输(必须要 不然会一直卡死)
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(path);
files = ftpClient.listFiles();
} catch (Exception e) {
log.error(ExceptionUtils.getStackTrace(e));
}
return files;
}
首先确认了FTP所需端口全部放行,并服务器已设置被动模式,再次尝试问题任然存在。
继续调试发现,在执行完 listFiles() 方法后 FTPClient 类中有一个内部属性 passiveHost会被赋值,这个属性存储的是被动模式(PASV)连接时的服务器主机地址,可以看到连接 FTP 时,指定的 Ip 地址是 19,而 passiveHost 中存储的地址是 64,而在执行 listFiles() 时使用的地址是passiveHost,所以会出现连接超时的异常。
Host
passiveHost
进一步查看代码发现 passivePort 属性基本都是使用当前 Socket 连接的远程主机的Ip地址,也就是说 Socket 解析到了错误的Ip。
this.getRemoteAddress().getHostAddress();
解决方法
解决方法可以通过 FileZilla 修改设置或使用工具类解决。
1. 修改设置
高版本 FileZilla,在设置 External Server IP Address for passive 时,不要使用 Default 模式,使用 Use the following Ip 或 Retrieve external Ip address from。
注:没有新版的 FileZilla 从别的老师的博客上截的图,清泉山。
低版本 FileZilla,在设置中找到 Protocols settings --> FTP and FTP over TLS (FTPS) --> Passive mode,
修改 **Use the following host (leave empty to keep the default one)😗*为指定的Ip
取消勾选 Use the default host for local connections 选项
2. 使用配置类
在 Apache Commons Net 的 FTPClient 类中,HostnameResolver 是一个接口,它用于解析服务器在被动模式下提供的 IP 地址。通常,当使用 FTP 的被动模式时,服务器会响应一个包含 IP 地址和端口号的 PASV 命令。这个 IP 地址可能是服务器的内部 IP 地址,对于客户端来说可能无法直接访问。因此,HostnameResolver 接口允许客户端自定义如何解析这个 IP 地址。
HostnameResolver 接口定义了一个方法 resolveHostname(String hostname),该方法接受一个字符串参数(即服务器提供的 IP 地址或主机名),并返回一个 InetAddress 对象,该对象表示客户端应该连接到的实际地址。
FTPClient 类在内部使用这个接口来解析服务器提供的地址。可以通过调用 FTPClient 的 setHostnameResolver 方法来提供自定义的 HostnameResolver 实现。
package i.kun;
import org.apache.commons.net.ftp.FTPClient;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @describe: 自定义 ftp 主机名称解析
* @author: xiaowuler
* @createTime: 2024-03-22 10:49
*/
public class CustomHostnameResolver implements FTPClient.HostnameResolver {
private final FTPClient client;
private String ip;
public CustomHostnameResolver(final FTPClient client, String ip) {
this.client = client;
this.ip = ip;
}
@Override
public String resolve(final String hostname) {
return ip;
}
}
3.使用方法
public static FTPClient connection(String hostname, int port, String username, String password) {
FTPClient ftp = new FTPClient();
CustomHostnameResolver customHostnameResolver = new CustomHostnameResolver(ftp, hostname);
ftp.setPassiveNatWorkaroundStrategy(customHostnameResolver);
try {
//连接FTP服务器
ftp.connect(hostname, port);
//下面三行代码必须要,而且不能改变编码格式,否则不能正确下载中文文件
ftp.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
//登录ftp
ftp.login(username, password);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
}
} catch (Exception e) {
log.error(ExceptionUtils.getStackTrace(e));
}
return ftp;
}
END…