Connect to FTP server


Hi, i am researching on how to build a simple FPT client application to connect to an a remote server and transfer files between them. Please suggest a good solution and/or you have example code please post here.

By vathana.serey asked on 2015-12-26
answer to this question

revathiganesh1
By revathiganesh1

File Transfer Protocol from one system to another by the way of server. Please explain me in detail about it

2016-04-06 Reply

yuk.dara
By yuk.dara

You can use FTPClient from Apache Commons library to connect to an FTP server and transfer file between Android devices and the server.
1. Here is an example code to connect to an FTP server from Android device.

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
.......................................................................
FTPClient client=new FTPClient();
public boolean connect(){
boolean connected=false;
String username="username";
String password="pwd";
String servername="FTP server name"
try {
//disconnect the previous connection
if(client.isConnected()){
client.logout();
client.disconnect();
}
//connect to the remove ftp server
client.connect(servername);
//log in to the server with user name and password
client.login(username, password);
//set the passive mode for the file transfer
client.enterLocalPassiveMode();
System.out.println(client.getReplyString());
int reply=client.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){ //connect an login successfully
connected=true;
}
else{
Toast.makeText(context, "Can't connect to the server", Toast.LENGTH_SHORT).show();
}

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

2. Upload files to server

public void upload(){
EditText txtLocalPath=(EditText)findViewById(R.id.txt_local);
String filepath=" Path to the file to upload";
File f=new File(filepath);
String filename=f.getName();
FileInputStream fis = null;
if(f.isFile()){
try {
fis = new FileInputStream(f);
client.storeFile(filename, fis);
fis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//background process for uploading file to server
private class BackTaskUpload extends AsyncTask<Void,Void,Void>{

protected void onPreExecute(){
super.onPreExecute();
//show process dialog
pd = new ProgressDialog(context);
pd.setTitle("Uploading the file");
pd.setMessage("Please wait.");
pd.setCancelable(true);
pd.setIndeterminate(true);
pd.show();
}
protected Void doInBackground(Void...params){
try{
//upload selected file to server
upload();
}catch(Exception e){
pd.dismiss(); //close the dialog if error occurs
}
return null;
}
protected void onPostExecute(Void result){
pd.dismiss();
}
}


3. Download files from the FTP server
 public void downloadFile(View view){
BackTaskDownload btdownload=new BackTaskDownload();
btdownload.execute(null,null,null);
}
public void download(){
FileOutputStream outfile=null;
EditText txtServer=(EditText)findViewById(R.id.txt_server);
String txtpath=" Path to file on server to download";
if(isServerFile(txtpath)){
String filename=getFileName(txtpath);
try {
outfile=new FileOutputStream(localpath+"/"+filename);
client.retrieveFile(filename, outfile);


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
outfile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//background process for downloading file from server
private class BackTaskDownload extends AsyncTask<Void,Void,Void>{

protected void onPreExecute(){
super.onPreExecute();
//show process dialog
pd = new ProgressDialog(context);
pd.setTitle("Downloading the file");
pd.setMessage("Please wait.");
pd.setCancelable(true);
pd.setIndeterminate(true);
pd.show();
}
protected Void doInBackground(Void...params){
try{
//upload selected file to server
download();
}catch(Exception e){
pd.dismiss(); //close the dialog if error occurs
}
return null;
}
protected void onPostExecute(Void result){
pd.dismiss();
}
}



2015-12-26 Reply





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.