I try to connect a FTP client from my Android device to a FTP server, hosted in a docker (via the stilliard/pure-ftpd image). Connection : Ok, Login : Ok, but when I try send a file, I have this exception from my Android device :
failed to connect to localhost/127.0.0.1 (port 30007): connect failed: ECONNREFUSED (Connection refused)
I tried with a "regular" FTP server (vsftpd, not in docker). It works.
I use the apache commons library, via gradle's dependencies.
compile group: 'commons-net', name: 'commons-net', version: '3.5'
Code use by the Android app (this code is a quick-written-testing-code) :
FTPClient ftp = new FTPClient();
try {
ftp.connect("myhostname", 21);
if (!ftp.login(mLogin, mPassword)) {
throw new RuntimeException();
}
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return false;
}
ftp.enterLocalPassiveMode();
if (!ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE)) {
throw new RuntimeException();
}
for (String docPath : docPaths) {
Log.d(TAG, "doInBackground: sending " + docPath + "...");
File file = new File(docPath);
InputStream inputStream = new FileInputStream(file);
if (!ftp.storeUniqueFile(file.getName(), inputStream)) {
Log.e(TAG, "doInBackground: error while sending");
ftp.disconnect();
return false;
}
inputStream.close();
}
if (!ftp.logout()) {
throw new RuntimeException();
}
ftp.disconnect();
return true;
} catch (Exception e) {
Log.e(TAG, "doInBackground: ", e);
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException e1) {
Log.e(TAG, "doInBackground: ", e1);
}
}
return false;
}