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