Getting connect failed: ETIMEDOUT (Connection timed out) in ftp connect

痞子三分冷 提交于 2019-12-10 17:29:57

问题


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


回答1:


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 


来源:https://stackoverflow.com/questions/25886578/getting-connect-failed-etimedout-connection-timed-out-in-ftp-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!