One of my recent tasks has been to download a file from an FTP server. The recommended solution is to use the Apache Commons Net library for that, which supports FTP among many others protocols. Adding it to the project was as easy as putting the jar into the libs/ directory.
Following a few examples, here is how to download a file:
When running on device, an exception gets thrown every time from the retrieveFile() function: java.net.SocketTimeoutException: Connection timed out, which seems strange because the connection is already established. Looking at the logs on the server, I saw the following commands coming: USER, PASS, TYPE I, and PASV, but there was no RETR command. Is this a bug in the library or …?
Our testing FTP server is located somewhere in corporate networks, which implies a lot of restrictions and firewalls. After some recollections on how FTP protocol works, I figured the problem was in the passive mode (http://www.ncftp.com/ncftpd/doc/misc/ftp_and_firewalls.html). The reply to the PASV command was like this: 227 Entering Passive Mode (172,168,20,1,204,173), which stated the IP address and port to connect to, but it was local (172.168.20.1), whereas the client had connected to the external IP of the server (like 88.88.88.88).