gpt4 book ai didi

android - 获取连接失败 : ETIMEDOUT (Connection timed out) in ftp connect

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:28 24 4
gpt4 key购买 nike

我一直在使用 ftp 在 android 应用程序的服务器上上传图像,我正在使用以下代码与 ftp 连接。它在 Wi-fi 中工作正常,但如果我切换到 3G 或 2G 连接,我会收到连接超时错误。那么请让我知道如何处理这种情况。我的客户在 Veriozon、Sprint、ATT 网络提供商中也面临这个问题。它的 iPhone 版本在所有网络中都运行良好。

代码:

try {
ftpClient = new FTPClient();
ftpClient.setConnectTimeout(30);
ftpClient.connect(InetAddress.getByName(server));

boolean isLogin = ftpClient.login(username, password);
boolean workingDir = ftpClient
.changeWorkingDirectory(path);

if (ftpClient.getReplyString().contains("250")) {

ftpClient
.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);

buffIn = new BufferedInputStream(
new FileInputStream(filePath));
ftpClient.enterLocalActiveMode();
// ftpClient.enterLocalPassiveMode();

ProgressInputStream progressInput = new ProgressInputStream(
buffIn, progressHandler);

isUploaded = ftpClient.storeFile(fileName,
progressInput);

buffIn.close();
ftpClient.logout();
ftpClient.disconnect();
}

} catch (Exception e) {
runOnUiThread(new Runnable() {

@Override
public void run() {
runOnUiThread(new Runnable() {

@Override
public void run() {
progressDialog.dismiss();
Toast.makeText(RegisterActivity.this,
R.string.postimage_uploaderror,
Toast.LENGTH_LONG).show();
}
});

}
});
}

错误:

java.net.ConnectException: failed to connect to Host (port 21): connect failed: ETIMEDOUT (Connection timed out)

我在我的项目中导入了“commons-net-ftp-2.0.jar”和commons-net-3.3.jar。

期待您的回答。

最好的问候,

德旺

最佳答案

通常 3G 或 2G 连接比 wifi 慢,这就是您收到连接超时错误的原因。要遇到这种情况,您需要为您的 FTP 客户端设置超时延迟,您可以通过添加此行来实现

ftpClient.setConnectTimeout(30); // 30 mSeconds increase it for more time 

所以你的代码将变成:

ftpClient.setConnectTimeout(30);
ftpClient.connect(InetAddress.getByName(server));
boolean isLogin = ftpClient.login(username, password);
boolean workingDir = ftpClient.changeWorkingDirectory(path);

编辑

将超时增加到 50 秒(50000)

ftpClient.setConnectTimeout(50000); // 50 Seconds increase it for more time 

关于android - 获取连接失败 : ETIMEDOUT (Connection timed out) in ftp connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25886578/

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