Tuesday, May 14, 2013

Android FTP Client

Android comes with HTTP Client class but not FTP, it’s a bit tricky to program FTP featured application without some third party FTP library. I tried the FTP client in Apache’s Jarkarta Commons Net.

Download and install the FTP Client library.
- Download the Jarkarta Commons Net (commons-net-2.0.zip) from

http://commons.apache.org/net/

- Unzip the zip file and find the following two jars. You only need the two jars and don’t include the others; otherwise, you will have class conflict.

commons-net-2.0.jar
commons-net-ftp-2.0.jar

- Assuming you are using Eclipse …
Create a lib folder in your app’s project and place the two jars in the lib folder.
Open your app’s Properties dialog, navigate to “Java Build Path”->”Libraries” and add the reference to the two jars.
Navigate to “Java Build Path”->”Order and Export” and select to export the two jars.


Using the FTPClient

a) Instantiates an FTPClient and connect to a server

mFtp=new FTPClient();
mFtp.connect(“ftp.domain.com”,21); // Using port no=21
mFtp.login(userid, pwd);

b) Putting a binary file to remote server

mFtp.setFileType(FTP.BINARY_FILE_TYPE);
mFtp.enterLocalPassiveMode();
boolean aRtn=mFtp.storeFile(remoteFileName, aInputStream);// return true if successful
aInputStream.close();

c) Disconnect from server
mFtp.disconnect();