我正在不同线程中的代码中打开多个FTP连接。 使用相同的凭据可以有两个连接到同一FTP服务器。
这会导致主动/被动反向通道传输数据出现问题吗?我遇到的实际问题是,即使登录之前在另一个线程中工作了几秒钟,该登录也会失败。很难复制。
另一个问题,主动还是被动是标准设置?哪个更好?
FTPClient ftp = new FTPClient();
ftp.setDefaultTimeout(timeoutMilliseconds);
ftp.setDataTimeout(timeoutMilliseconds);
ftp.setConnectTimeout(timeoutMilliseconds);
//ftp.setSoTimeout(1000);
//ftp.setControlKeepAliveTimeout(1000);
//ftp.setControlKeepAliveReplyTimeout(1000);
ftp.connect(serverAddress, serverPort);
logger.info("Connected successfully to " + serverAddress + ":" + serverPort);
boolean login = ftp.login(username, password);
logger.info("Logged in successfully to " + serverAddress + ":" + serverPort);
ftp.disconnect();
答案 0 :(得分:0)
建议使用被动模式。请找到我在项目中使用的以下设置。您可以参考https://www.jscape.com/blog/bid/80512/active-v-s-passive-ftp-simplified了解更多详细信息。
FTPClient ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
ftp.connect(host, port);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new IllegalArgumentException("Not able to connect to the ftp Server ");
}
ftp.login(userName, password);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
ftp.setBufferSize(100000);
ftp.setConnectTimeout(1200000);
ftp.setDataTimeout(1200000);
ftp.setDefaultTimeout(1200000);