连接失败:ETIMEDOUT(连接超时)以ftp连接 [英] Getting connect failed: ETIMEDOUT (Connection timed out) in ftp connect

查看:8802
本文介绍了连接失败:ETIMEDOUT(连接超时)以ftp连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用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);
布尔workingDir = ftpClient
.changeWorkingDirectory(路径); $(b)b
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(){
$ b $ @Override
public void run {
runOnUiThread(new Runnable(){
$ b $ @Override
public void run(){
progressDialog.dismiss();
Toast.makeText RegisterActivity.this,
R.string.postimage_uploaderror,
Toast.LENGTH_LONG).show();
}
});

}
});

错误:


java.net.ConnectException:无法连接到主机(端口21):
连接失败:ETIMEDOUT(连接超时)


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

期待您的回答。



祝好,

<通常3G或2G连接比WiFi慢,这就是为什么你会收到连接超时错误的原因。遇到这种情况,您需要为您的FTP客户端设置超时延迟,并且您可以通过添加此行来完成此操作。

  ftpClient.setConnectTimeout (30); // 30 mSeconds增加更多时间

所以你的代码会变成:

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

编辑

将暂停时间增加至50秒(50000)

  ftpClient.setConnectTimeout(50000); // 50秒增加更多时间


I have been using ftp to upload images on server in android application and I'm using the following code to connect with ftp. it's working fine in Wi-fi but if I switched to 3G or 2G connection, I am getting connection time out error. So would you please let me know how to take care of this situation. And my client is also facing this issue in Veriozon, Sprint, ATT network provider too. It's iPhone version is working fine in all network.

Code :

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

                        }
                    });
                }

Error :

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

I have imported "commons-net-ftp-2.0.jar" and commons-net-3.3.jar in my project.

Looking forward for your answer.

Best Regards,

Devang

解决方案

Normally a 3G or 2G connection is slower than wifi this is why you get a Connection Timeout error. to encounter this you need to set the timeout delay for your FTP client and you can do it by adding this line

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

so your code will become :

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

EDIT

increase the timeout to 50s (50000)

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

这篇关于连接失败:ETIMEDOUT(连接超时)以ftp连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆