Using apache commons Ftp library in Android

1) Download commons-net-2.0-src files

Download the source files or commons-net-2.0-src.tar.gz from I tried with the binaries version containing only the jar files. But when i add the jar files to my Android project, I got the error: "Conversion to Dalvik format failed with error 1".

2) Extract and copy Ftp and dependent source files to Android project

Extract the compressed source files. The files will be extracted to commons-net-2.0-src folder. Go to commons-net-2.0-src | src | main | java | org | apache | commons | net folder. Copy all .java files in the net folder and the subfolders ftp, io, util. Am copying only files required for Ftp to work. If need other network protocols then copy the corresponding subfolders also.

In Android project's src folder, create the folders org | apache | commons | net and paste the files and subfolders copied above in the newly created net folder.

Here's how it looks like in eclipse package explorer:

Note: There may be other better ways to import external source files to an eclipse project. But am quite new to eclipse. So the first simplest way i could come up now is to just copy the source files needed into Android's src folder.

3) Using the Ftp library for file tranfer

You are now ready to use the classes available from apache commons Ftp library. Below is a very simple basic code example for uploading file from Android to an Ftp server. For more functionalities, refer to the api doc from apache commons project or just do a google search on how to use the Ftp classes from apache commons.

package laval.apacheftpclient;



import android.os.Bundle;

public class ApacheFTPClient extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        FTPClient mFTP = new FTPClient();
        try {
            // Connect to FTP Server
            mFTP.login("user", "password");
            // Prepare file to be uploaded to FTP Server
            File file = new File("/path/to/filetotranfer");
            FileInputStream ifile = new FileInputStream(file);
            // Upload file to FTP Server
        } catch (SocketException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block

0 Response to "Using apache commons Ftp library in Android"

Post a Comment