I ran into trouble while writing a piece of code to transfer a bunch of images to my server from a custom app. People recommended Apache FTP for Java. But it can't be used as is since Honeycomb. It needs the task to run on a different thread. This makes the UI far more responsive and is a forced better way to write programs for Android.
Step 1:
First download the zip file called Apache Commons Net from
http://commons.apache.org/net/download_net.cgi
Step 2:
Extract the zip file and copy the file called commons-net-xx.jar Into the libs folder of your Android eclipse project
Step 3: Create a new Java Class with the following code.
package com.example.technologyempoweredjanatainitiative;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import android.os.AsyncTask;
import android.util.Log;
public class FTP_Uploader_AsyncTask extends AsyncTask {
//void FTP_DATA_UPLOAD(String FULL_PATH_TO_LOCAL_FILE)
protected Long doInBackground(String... FULL_PATH_TO_LOCAL_FILE ) {
// encapsulate FTP inside a A sync task
{
System.out.println("Entered FTP transfer function");
FTPClient ftpClient = new FTPClient();
int reply;
try {
System.out.println("Entered Data Upload loop!");
ftpClient.connect("Ftp.example.com",21);
ftpClient.login("username", "password");
ftpClient.changeWorkingDirectory("/directory/");
System.out.println("Entered Data Upload loop!");
reply = ftpClient.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){
System.out.println("Connected Success");
}else {
System.out.println("Connection Failed");
ftpClient.disconnect();
}
if (ftpClient.getReplyString().contains("250")) {
ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
BufferedInputStream buffIn = null;
System.out.println("Created an input stream buffer");
System.out.println(FULL_PATH_TO_LOCAL_FILE.toString());
buffIn = new BufferedInputStream(new FileInputStream(FULL_PATH_TO_LOCAL_FILE[0]));
ftpClient.enterLocalPassiveMode();
System.out.println("Entered binary and passive modes");
//Handler progressHandler=null;
//ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler);
//Code to Extract name from the string.
//http://stackoverflow.com/questions/10549504/obtain-name-from-absolute-path-substring-from-last-slash-java-android
String Picture_File_name = new File(FULL_PATH_TO_LOCAL_FILE[0]).getName();
boolean result = ftpClient.storeFile(Picture_File_name, buffIn); //localAsset.getFileName()
//ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler);
if (result){
System.out.println("Success");
}
//boolean result = ftpClient.storeFile("TEST.jpg", progressInput);
System.out.println("File saved");
//buffIn.close();
ftpClient.logout();
ftpClient.disconnect();
}
} catch (SocketException e) {
Log.e("Citizen Cam FTP", e.getStackTrace().toString());
System.out.println("Socket Exception!");
} catch (UnknownHostException e) {
Log.e("Citizen Cam FTP", e.getStackTrace().toString());
} catch (IOException e) {
Log.e("Citizen Cam FTP", e.getStackTrace().toString());
System.out.println("IO Exception!");
}
return null;
}
}
}
Step 1:
First download the zip file called Apache Commons Net from
http://commons.apache.org/net/download_net.cgi
Step 2:
Extract the zip file and copy the file called commons-net-xx.jar Into the libs folder of your Android eclipse project
Step 3: Create a new Java Class with the following code.
package com.example.technologyempoweredjanatainitiative;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import android.os.AsyncTask;
import android.util.Log;
public class FTP_Uploader_AsyncTask extends AsyncTask
//void FTP_DATA_UPLOAD(String FULL_PATH_TO_LOCAL_FILE)
protected Long doInBackground(String... FULL_PATH_TO_LOCAL_FILE ) {
// encapsulate FTP inside a A sync task
{
System.out.println("Entered FTP transfer function");
FTPClient ftpClient = new FTPClient();
int reply;
try {
System.out.println("Entered Data Upload loop!");
ftpClient.connect("Ftp.example.com",21);
ftpClient.login("username", "password");
ftpClient.changeWorkingDirectory("/directory/");
System.out.println("Entered Data Upload loop!");
reply = ftpClient.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){
System.out.println("Connected Success");
}else {
System.out.println("Connection Failed");
ftpClient.disconnect();
}
if (ftpClient.getReplyString().contains("250")) {
ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
BufferedInputStream buffIn = null;
System.out.println("Created an input stream buffer");
System.out.println(FULL_PATH_TO_LOCAL_FILE.toString());
buffIn = new BufferedInputStream(new FileInputStream(FULL_PATH_TO_LOCAL_FILE[0]));
ftpClient.enterLocalPassiveMode();
System.out.println("Entered binary and passive modes");
//Handler progressHandler=null;
//ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler);
//Code to Extract name from the string.
//http://stackoverflow.com/questions/10549504/obtain-name-from-absolute-path-substring-from-last-slash-java-android
String Picture_File_name = new File(FULL_PATH_TO_LOCAL_FILE[0]).getName();
boolean result = ftpClient.storeFile(Picture_File_name, buffIn); //localAsset.getFileName()
//ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler);
if (result){
System.out.println("Success");
}
//boolean result = ftpClient.storeFile("TEST.jpg", progressInput);
System.out.println("File saved");
//buffIn.close();
ftpClient.logout();
ftpClient.disconnect();
}
} catch (SocketException e) {
Log.e("Citizen Cam FTP", e.getStackTrace().toString());
System.out.println("Socket Exception!");
} catch (UnknownHostException e) {
Log.e("Citizen Cam FTP", e.getStackTrace().toString());
} catch (IOException e) {
Log.e("Citizen Cam FTP", e.getStackTrace().toString());
System.out.println("IO Exception!");
}
return null;
}
}
}
To access the ftp service to upload files call the above class using :
new FTP_Uploader_AsyncTask().execute(PathForFileToUpload);