Howto Do a Simple Ftp Get File on Android

Howto do a simple ftp get file on Android

Whew! I finally got it going. I gave up on the simple way that works in webOS and WPF/C# where you can just do a ftp:://... you have to use the FTPClient package.

After fixing the library access (Project | Properties | Java Build Path | Libraries | Add JARs...) I fiddled with the calls until it started working. Here's the sequence of my FTPClient calls. It wouldn't work until I set it in passive mode.

  mFTPClient = new FTPClient();
mFTPClient.connect("tgftp.nws.noaa.gov");
mFTPClient.login("anonymous","nobody");
mFTPClient.enterLocalPassiveMode();
mFTPClient.changeWorkingDirectory("/data/forecasts/taf/stations");
InputStream inStream = mFTPClient.retrieveFileStream("KABQ.TXT");
InputStreamReader isr = new InputStreamReader(inStream, "UTF8");

Reading txt file from an ftp server and returning it from AsyncTask

There are many ways to fully read an InputStream into a String. The easiest one would be to use IOUtils.toString() from Apache Commons IO.

So, assuming that, the changes would be to return said string from doInBackground() and receive it in onPostExecute().

Fetch Android files using FTP and Python

ftp = FTP('123.456.7.89:13000', 'username', 'password')

The documentation of ftplib suggests that the first argument must be a host, not host:port. This explains also the following error:

[Errno 11004] getaddrinfo failed

This is an error from the resolver, because it tried to interpret the given name 123.456.7.89:13000 as hostname, IPv4 (or IPv6) and none of this worked.

If you use a non-standard port you have to do connect and login after you have created the FTP object, because there is no way to give a non-standard port to the object constructor:

 ftp = FTP()
ftp.connect('123.456.7.89',13000)
ftp.login(username,password)

Listening files from FTP server in android

Download the apache FTP library from http://www.docjar.com/jar_detail/commons-net-ftp-2.0.jar.html and add it to your libs. Now check the below code.

ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
FTPClient ftpclient = new FTPClient();
ftpclient.connect(server, portnumber);
ftpclient.login(username, password);
ftpclient.setFileType(FTPClient.BINARY_FILE_TYPE);
FTPFile[] ftpFiles = ftpclient.listFiles(dir_path);
for (FTPFile file : ftpFiles) {
Log.i("TAG", file.getName());
listItems.add(file.getName());
}
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adapter);
ftpClient.logout();
ftpClient.disconnect();

Android FTP upload - Files are not uploaded

I fixed the Problem. It was a copy and past fail in the FtpDataHandler.
I set the this.serverDirectory equals serverAdress. So the Ftp Client could not find the directory on the server.

Here is the code snippit:

public FtpDataHandler(String serverAdress, String userName, String password, String serverDiretory) {
this.serverAdress = serverAdress;
this.userName = userName;
this.password = password;
//this was my problem. I set the serverAdress to the serverDirectory insted the serverDirectory
//this.serverDirectory = serverAdress;
this.serverDirectory = serverDirectory;

connect();
}

I also fixed it in the question, so that it's easier to find the full solution.

Thanks to @Martin Prikryl for the support.



Related Topics



Leave a reply



Submit