Using apache commons Ftp library in Android

1) Download commons-net-2.0-src files

Download the source files commons-net-2.0-src.zip or commons-net-2.0-src.tar.gz from http://commons.apache.org/net/download_net.cgi. 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 java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import android.app.Activity;
import android.os.Bundle;

public class ApacheFTPClient extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        FTPClient mFTP = new FTPClient();
        try {
            // Connect to FTP Server
            mFTP.connect("192.168.1.110");
            mFTP.login("user", "password");
            mFTP.setFileType(FTP.BINARY_FILE_TYPE);
            mFTP.enterLocalPassiveMode();
            
            // 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
            mFTP.storeFile("filetotranfer",ifile);
            mFTP.disconnect();          
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

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

Post a Comment