Reputation: 342
I'm using this to upload some file. It works if I in a local connection, but if I use a external connection, i get this message: 425 Can't open data connection.
from the ftp server.
I use the org.apache.commons.net.ftp.FTPClient and org.apache.commons.net.ftp.FTPFile libs.
public static String gravaImagem(String photoFile) {
FTPClient mFtp = new FTPClient();
try {
mFtp.connect(FTPHOST, PORTA);
mFtp.login(USUARIO, SENHA);
mFtp.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
mFtp.setFileType(FTPClient.BINARY_FILE_TYPE);
String origem = Environment.getExternalStorageDirectory().getPath()+File.separator+"Pictures"+File.separator+"ImageSec"+File.separator+photoFile;
FileInputStream fis = new FileInputStream(origem);
mFtp.storeFile(photoFile, fis);
mFtp.logout();
mFtp.disconnect();
} catch (SocketException e) {
e.printStackTrace();
return "Fail. (ERR#CON3)";
} catch (IOException e) {
e.printStackTrace();
return "Fail. (ERR#CON4)";
}
return "Imagem enviada ao servidor.";
}
Debug shows no exceptions.
Upvotes: 1
Views: 3237
Reputation: 9569
From the internet:
First - the most common solution: change the active/passive mode settings. But that might not work, and if it does its only a band-aid covering up the real problem.
As I've mentioned in the past, one of the most common reasons that this error occurs is a misconfiguration of the FTP server software itself, related to SSL connections and firewalls, in which the connection tries to establish itself on a bogus ip address. Read more about FTP SSL through a NAT firewall here, some potential solutions are included.
There are other less likely causes, such as:
- The server is configured to always use the same port for passive mode connections, or the client is configured to always use the same port for active mode connections, although in this case usually the software in question should raise a different error first, but I've seen this happen.
- In passive mode, the firewall in front of the FTP server doesn't have the correct ports open. So the server tells the client to connect to ipaddress 1.2.3.4 on port x, but the firewall doesn't allow incoming connections on port x. Most firewalls are smart enough to open up the port when it sees the PASV response. Vice versa for active mode and the firewall in front of the FTP client.
From me: I've used this library on andoird and it worked well, so see my copy/paste section.
Upvotes: 1