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:
- 1st packet will contain the name of the file.
- 2nd packet will contain the length of the file.
- 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
Preparedstatement With List of Parameters in a in Clause
Find Java_Home and Set It on Rhel
Why How to Successfully Move a File in Linux While It Is Being Written To
List Contents of Multiple Jar Files
The Full Pathname of a Jdk Installation for Oracle SQL Developer
How to Add Multiple Jar Files in Classpath in Linux
Create a Java Executable with Eclipse
Installing Jdk8 on Ubuntu- "Unable to Locate Package" Update Doesn't Fix
How to Make the Method Return Type Generic
Gson: How to Exclude Specific Fields from Serialization Without Annotations
Remote Debugging a Java Application
Difference Between Getattribute() and Getparameter()
How to Sort Map Values by Key in Java
Can the Java Cookbook Be Used to Install a Local Copy of Oracle Java