FTP File Upload From Sdcard to server

DOWNLOAD CODE

Related Examples

First Time User Please check previous example for normal file upload by server script( php|java|.net|coldfusion )
Upload File To Server - Android Example

 

In this example uploading file on server by ftp client ( with the use of ftp4j-1.6.jar ).

Steps :

   1. Using ftp4j-1.6.jar from sauronsoftware repository ( http://www.sauronsoftware.it/projects/home/ ). Download and Add ftp4j-1.6.jar as library.
   2. Must be required a Server Dedicated IP for FTP File upload.

 

Project Structure :

 

FTP_Upload_file_project_sketch

 

Add Library ftp4j-1.6.jar :

 

FTP_Upload_file_add_library_ftp4j

 

File : AndroidMainifest.xml

 


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.kpbird.ftpdemo"
      android:versionCode="1"
      android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".FtpUpload"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

 

File : src/FtpUpload.java

 

ftp upload file from sdcard to webserver.

 


import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class FtpUpload extends Activity implements OnClickListener {
    
	
	/*********  work only for Dedicated IP ***********/
	static final String FTP_HOST= "50.63.92.56";
	
	/*********  FTP USERNAME ***********/
	static final String FTP_USER = "XXXXXX";
	
	/*********  FTP PASSWORD ***********/
	static final String FTP_PASS  ="XXXXXXX";
	
	Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(this);
        
    }
    
    public void onClick(View v) {
		
    	/********** Pick file from sdcard *******/
		File f = new File("/sdcard/logo.png");
		
		// Upload sdcard file
		uploadFile(f);
		
	}
    
    public void uploadFile(File fileName){
    	
    	
		 FTPClient client = new FTPClient();
		 
		try {
			
			client.connect(FTP_HOST,21);
			client.login(FTP_USER, FTP_PASS);
			client.setType(FTPClient.TYPE_BINARY);
			client.changeDirectory("/upload/");
			
			client.upload(fileName, new MyTransferListener());
			
		} catch (Exception e) {
			e.printStackTrace();
			try {
				client.disconnect(true);	
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		
    }
    
    /*******  Used to file upload and show progress  **********/
    
    public class MyTransferListener implements FTPDataTransferListener {

    	public void started() {
    		
    		btn.setVisibility(View.GONE);
    		// Transfer started
    		Toast.makeText(getBaseContext(), " Upload Started ...", Toast.LENGTH_SHORT).show();
    		//System.out.println(" Upload Started ...");
    	}

    	public void transferred(int length) {
    		
    		// Yet other length bytes has been transferred since the last time this
    		// method was called
    		Toast.makeText(getBaseContext(), " transferred ..." + length, Toast.LENGTH_SHORT).show();
    		//System.out.println(" transferred ..." + length);
    	}

    	public void completed() {
    		
    		btn.setVisibility(View.VISIBLE);
    		// Transfer completed
    		
    		Toast.makeText(getBaseContext(), " completed ...", Toast.LENGTH_SHORT).show();
    		//System.out.println(" completed ..." );
    	}

    	public void aborted() {
    		
    		btn.setVisibility(View.VISIBLE);
    		// Transfer aborted
    		Toast.makeText(getBaseContext()," transfer aborted ,
 please try again...", Toast.LENGTH_SHORT).show();
    		//System.out.println(" aborted ..." );
    	}

    	public void failed() {
    		
    		btn.setVisibility(View.VISIBLE);
    		// Transfer failed
    		System.out.println(" failed ..." );
    	}

    }
}