Upload image to the server using ftp in Android

by / Monday, 23 September 2013 / Published in Android

Here we learn how to upload images to the server using ftp in android.

Here FTPClient takes a major role in this example.
public class FTPClient extends FTP implements ConfigurableFTPClient :
FTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server. This class takes care of all low level details of interacting with an FTP server and provides a convenient higher level interface. As with all classes derived from SocketClient, you must first connect to the server with connect before doing anything, and finally disconnect after you’re completely finished interacting with the server. Then you need
to check the FTP reply code to see if the connection was successful.

ImageUploadActivity.java :

package in.mettletech.imageuploadtoserver;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ImageUploadActivity extends Activity {
String desDirectory="destination path";
String filename="icon.png";
String sourceUrl="/mnt/sdcard/";
Button uploagimage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_upload);
uploagimage=(Button) findViewById(R.id.uploadimage);
uploagimage.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
if(isNetworkAvailable())
{
new UploadTask().execute();
}else{
AlertDialog.Builder b=new AlertDialog.Builder(ImageUploadActivity.this);
b.setMessage("Internet connectivity failure.Try again!");
b.show();
}

}
});
}

class UploadTask extends AsyncTask<Void, Void, Void> {

ProgressDialog pDialog;
Boolean uploadStat;

@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(ImageUploadActivity.this);
pDialog.setMessage("Uploading...");
pDialog.show();
super.onPreExecute();
}

@Override
protected Void doInBackground(Void... params) {
uploadStat=new UploadToFtp().ftpUpload1(sourceUrl, filename, desDirectory);
runOnUiThread(new Runnable() {

@Override
public void run() {
if(uploadStat)
{
AlertDialog.Builder b=new AlertDialog.Builder(ImageUploadActivity.this);
b.setMessage("Upload Successful");
b.show();
}else{
AlertDialog.Builder b=new AlertDialog.Builder(ImageUploadActivity.this);
b.setMessage("Upload Failed.");
b.show();
}

}
});
return null;
}

@Override
protected void onPostExecute(Void result) {
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
super.onPostExecute(result);
}
}

// Check Internet Connection!!!

public boolean isNetworkAvailable() {
ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
}

UpLoadToFtp.java :

package in.mettletech.imageuploadtoserver;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import android.app.ProgressDialog;
import android.widget.ProgressBar;

public class UploadToFtp {
public FTPClient mFTPClient = null;
public boolean ftpUpload1(String srcFilePath,String desFileName,String desDirectory)
{
String host="hostname";
String username="username";
String password="password";
int port=21;
mFTPClient = new FTPClient();
boolean status = false;
try {

mFTPClient.connect(host, port);  // connecting to the host
mFTPClient.login(username, password);  //Authenticate using username and password
mFTPClient.changeWorkingDirectory(desDirectory); //change directory to that directory where image will be uploaded

mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
BufferedInputStream buffIn = null;
File file=new File(srcFilePath+desFileName);
buffIn = new BufferedInputStream(new FileInputStream(file),8192);
mFTPClient.enterLocalPassiveMode();
status = mFTPClient.storeFile(desFileName, buffIn);

buffIn.close();
mFTPClient.logout();
mFTPClient.disconnect();

} catch (Exception e) {
e.printStackTrace();
}
return status;
}
}

 

Tagged under: ,
TOP