Android Ftp Library

Android FTP Library

Try using apache commons ftp

FTPClient ftpClient = new FTPClient();
ftpClient.connect(InetAddress.getByName(server));
ftpClient.login(user, password);
ftpClient.changeWorkingDirectory(serverRoad);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

BufferedInputStream buffIn = null;
buffIn = new BufferedInputStream(new FileInputStream(file));
ftpClient.enterLocalPassiveMode();
ftpClient.storeFile("test.txt", buffIn);
buffIn.close();
ftpClient.logout();
ftpClient.disconnect();

FTP - android client and java server

The same way you are sending the messages from Android to PC will be used to send a file from android to PC. You will have to use byte arrays to send the messages from android to PC.

For example you can define:

  1. 1st packet will contain the name of the file.
  2. 2nd packet will contain the length of the file.
  3. 3rd packet will contain 1st packet of the file...
    .
    .

Similarly you can then send the whole file to your PC Server. I hope this will help

For Java FTP Server:

http://mina.apache.org/ftpserver-project/embedding_ftpserver.html

For Android FTP Client As manoj mentioned earlier in his comments:

Android FTP Library

I can't connect to host of my FTP server with FTPClient in Java-Android

I have found the problem!
The connexion at an FTP server must be making in a async task!

FTPClient doesn't like to work in the main thread, so we must make work in an another thread.

package com.example.erwan.ftplearn;

import android.os.AsyncTask;
import android.util.Log;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;

import java.io.IOException;
import java.net.InetAddress;

public class FTPModel {
public FTPClient mFTPClient = null;

public boolean connect(String host, String username, String password, int port)
{
try
{
return new asyncConnexion(host, username, password, port).execute().get();
}
catch (Exception e)
{
return false;
}
}

public class asyncConnexion extends AsyncTask<Void, Void, Boolean>
{
private String host;
private String username;
private String password;
private int port;

asyncConnexion(String host, String username, String password, int port)
{
this.host = host;
this.password = password;
this.port = port;
this.username = username;
}


@Override
protected Boolean doInBackground(Void... voids) {
try {

mFTPClient = new FTPClient();
// connecting to the host
mFTPClient.connect(host, port);

// now check the reply code, if positive mean connection success
boolean status = mFTPClient.login(username, password);

mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();
return status;

} catch (Exception e) {
Log.i("testConnection", "Error: could not connect to host " + host);
e.printStackTrace();

}
return false;
}
}
}


Related Topics



Leave a reply



Submit