Reputation: 1
First of all - This is my first question I ask on StackOverflow. And I'm from Germany, my english is not so good :)
I try to create a FTP Client as a Android App. I'm coding with Eclipse and the Android SDK.
This is my Code, but it doesn't work. I use the Apache Commons FTP Library. Can you help me? I don't want a functional Code, but I love to get Advice to get the Code working. Thanks!
So here is my code:
import org.apache.commons.net.ftp.FTPClient;
public class speechatStart extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.bt_load);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FTPClient client = new FTPClient();
TextView ausgabe = (TextView) findViewById(R.id.ausgabe);
try {
client.connect("ftp-web.ohost.de");
client.login("ftp1857836", "123456789");
String filename = "file1.txt";
FileInputStream fis = null;
fis = new FileInputStream(filename);
client.storeFile(filename, fis);
client.logout();
fis.close();
ausgabe.setText(fis);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ausgabe.setText("SocketException");
} catch (IOException e) {
// TODO Auto-generated catch block
ausgabe.setText("IOException");
}
}
});
Upvotes: 0
Views: 12802
Reputation: 917
Apache-Commons FTp library didn't gave reliable solution for me.So that I have used ftp4j which give me better solution and API is also much simple.
Example:
FTPClient client = new FTPClient();
client.connect("ftp.host.com");
if(client.isConnected())
{
client.login("username","password");
if(client.isAuthenticated())
{
client.upload(new java.io.File("localFile.txt"));
}
}
Hope this helps
Upvotes: 1
Reputation: 601
If you want to download file try next code:
ftpClient.retrieveFile(filename, outputStream);
outputStream.flush();
outputStream.close();
ftpClient.logout();
ftpClient.disconnect();
To upload change to
ftpClient.storeFile(filename, inputStream);
It seems that you are doing logout before stream is closed.
Upvotes: 0