简体   繁体   English

打开多个FTP连接

[英]Opening multiple FTP connections

I'm opening multiple FTP connections in my code in different threads. 我在不同线程的代码中打开多个FTP连接。 There can be two connections to the same FTP server using the same credentials. 可以使用相同的凭据到同一FTP服务器建立两个连接。

Can this cause a problem with the active/passive backchannel transfering the data? 这会导致主动/被动反向通道传输数据出现问题吗? The actual problem I'm having is that a certain login fails even though it worked seconds before in a different thread. 我遇到的实际问题是,即使登录之前在另一个线程中工作了几秒钟,该登录也会失败。 It is hard to reproduce. 很难复制。

Another question, is active or passive the standard setting? 另一个问题,主动还是被动是标准设置? Which one is better? 哪一个更好?

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();

Passive mode is recommended. 建议使用被动模式。 Please find the below settings that I have used in my project. 请找到我在项目中使用的以下设置。 You can refer to the https://www.jscape.com/blog/bid/80512/active-vs-passive-ftp-simplified for further details. 您可以参考https://www.jscape.com/blog/bid/80512/active-vs-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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM