Open your favorite IDE for android development and create a new project. Unless you choose to implement RFC 959 on your own, include a good ftp client library like this one http://commons.apache.org/proper/commons-net/download_net.cgi
If you already know how to include a jar in your android project, skip the following bullets.
- For android studio fans: copy-paste jar to libs folder of your project, right click, “add as library”. If you don’t see the libs folder check this link @ stackoverfow.
- For eclipse fans, see here.
Update your manifest.
<uses-permission android:name="android.permission.INTERNET"/>
Create an activity, add a button and program button’s click handler:
public class MyActivity extends Activity { protected void onCreate(Bundle b) { super.onCreate(b); setContentView(R.layout.content_layout_id); final Button button = (Button) findViewById(R.id.button_id); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { FtpConnection c = new FtpConnection(); c.password = "ftp_password"; c.port = 21; c.server = "ftp.example.com"; c.username = "ftp_username"; FtpAction a = new FtpAction(); a.upload = true; a.connection = c; a.remoteFilename = "remotefilename.bin"; new FtpAsyncTask(MyActivity.this).execute(a); } }); } } }
Click handler fires an async task that handles the communication with ftp server. There are 3 important entities in the above snippet: FtpConnection, FtpAction, FtpAsyncTask. We will see all of them in the following paragraphs.
Android prevents us from running network operations on main thread. This is the reason why we need an AsyncTask to enable background ftp communication. Thus, UI remains responsive, and if we implement so, user can be informed about task’s progress.
FtpAsyncTask snippet:
import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; import android.widget.Toast; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import java.io.ByteArrayInputStream; import java.io.InputStream; public class FtpAsyncTask extends AsyncTask<FtpAction, Void, Boolean> { private Exception exception; /** progress dialog to show user that the backup is processing. */ private ProgressDialog dialog; /** application context. */ private Context context; protected void onPreExecute() { this.dialog.setMessage("Starting"); this.dialog.show(); } @Override protected void onPostExecute(final Boolean success) { if (dialog.isShowing()) { dialog.dismiss(); } if (success) { Toast.makeText(context, "OK", Toast.LENGTH_LONG).show(); } else { Toast.makeText(context, "Error", Toast.LENGTH_LONG).show(); } } public FtpAsyncTask(Context context) { this.context = context; dialog = new ProgressDialog(context); } protected Boolean doInBackground(FtpAction... actions) { FTPClient ftp = new FTPClient(); FtpAction a = actions[0]; try { ftp.connect(a.connection.server, a.connection.port); ftp.login(a.connection.username, a.connection.password); boolean success = false; byte[] test = new byte[]{0, 1, 0, 1, 0, 1, 0, 1}; InputStream inputStream = new ByteArrayInputStream(test); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); success = ftp.storeFile(a.remoteFilename, inputStream); inputStream.close(); a.success = success; ftp.disconnect(); return success; } catch (Exception x) { this.exception = x; a.success = false; return false; } } }
FtpAsyncTask extends AsyncTask by implementing doInBackground. This method uses ftp library to communicate with ftp server. In our example snippet, we create an 8-byte input stram and we upload it to ftp server with the name “test.bin”. Our snippet fires a progress dialog that informs user for the operation in progress. If something goes wrong an error message pops up (Toast).
FtpConnection and FtpAction snippet:
public class FtpConnection { public String server; public int port; public String username; public String password; } public class FtpAction { public FtpConnection connection; public String remoteFilename; public boolean success = false; }
Leave a Reply