Your source code is now downloading. If does not start download please wait for 10 seconds

Your source code is now downloading. If does not start download please wait for 10 seconds

Created By Tejas Jasani | Last Updated on : 20 September 2017

Objective Main objective of this post is to give an idea about how to start FTP in Android

What is FTP ?

File Transfer Protocol (FTP) is a standard network protocol used to transfer files from one host to another host over a TCP-based network, such as the Internet. FTP is built on a client-server architecture and uses separate control and data connections between the client and the server. FTP users may authenticate themselves using a clear-text sign-in protocol, normally in the form of a username and password, but can connect anonymously if the server is configured to allow it.

For secure transmission that hides (encrypts) the username and password, and encrypts the content, FTP is often secured with SSL/TLS ("FTPS"). SSH File Transfer Protocol ("SFTP") is sometimes also used instead, but is technologically different.

Following are the steps to start FTP in Android device:

Step 1 Download Required .jar files.

First you need following JAR file:

1. commons-net-3.3.jar

Download latest jar file and add into libs folder of your Android project.

Step 2 MyFTPClientFunctions class

Create new class MyFTPClientFunctions into your project. Now add some FTP functions into newly created class.

public FTPClient mFTPClient = null; // Add top of the class

Now add method to connect FTP server. (Method to connect to FTP server)

public boolean ftpConnect(String host, String username, String password, int port) { try { mFTPClient = new FTPClient(); // connecting to the host mFTPClient.connect(host, port); // now check the reply code, if positive mean connection success if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) { // login using username & password boolean status = mFTPClient.login(username, password); /* * Set File Transfer Mode * To avoid corruption issue you must specified a correct * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE, * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE for * transferring text, image, and compressed files. */ mFTPClient.setFileType(FTP.BINARY_FILE_TYPE); mFTPClient.enterLocalPassiveMode(); return status; } } catch (Exception e) { Log.d(TAG, "Error: could not connect to host " + host); } return false; }

Now add method to disconnect FTP server. (Method to disconnect from FTP server)

public boolean ftpDisconnect() { try { mFTPClient.logout(); mFTPClient.disconnect(); return true; } catch (Exception e) { Log.d(TAG, "Error occurred while disconnecting from ftp server."); } return false; }

Now add method to upload file on FTP server.

public boolean ftpUpload(String srcFilePath, String desFileName, String desDirectory, Context context) { boolean status = false; try { FileInputStream srcFileStream = new FileInputStream(srcFilePath); // change working directory to the destination directory // if (ftpChangeDirectory(desDirectory)) { status = mFTPClient.storeFile(desFileName, srcFileStream); // } srcFileStream.close(); return status; } catch (Exception e) { e.printStackTrace(); Log.d(TAG, "upload failed: " + e); } return status; }

More methods are in full demo project.

Step 3 Call FTP Functions

Calling of above FTP functions. Create new class which extend Activity.

Add following into your class:

private MyFTPClientFunctions ftpclient = null;

Now add following into onCreate:

ftpclient = new MyFTPClientFunctions();

For start FTP connection:

Note Every FTP connection call in new Thread

new Thread(new Runnable() { public void run() { boolean status = false; // host – your FTP address // username & password – for your secured login // 21 default gateway for FTP status = ftpclient.ftpConnect(host, username, password, 21); if (status == true) { Log.d(TAG, "Connection Success"); } else { Log.d(TAG, "Connection failed"); } } }).start();

For terminate FTP connection:

new Thread(new Runnable() { public void run() { ftpclient.ftpDisconnect(); } }).start();

If you have got any query related handling FTP in Android comment them below. Other FTP functions are in full demo project.

Got an Idea of Android App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Android App Development Company in India.