Howto do a simple ftp get file on Android
Asked Answered
C

3

6

I can't find an example of a simple FTP access of a file anywhere, and the FTPClient class (which a couple of examples use) doesn't appear in the Android Class Index. I've got http access working, but how do I do a simple FTP get? All I want to do is download (for example): ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KABQ.TXT It shouldn't require login, change directory, etc. Just giving that URL to the http access methods don't seem to work.

This is similar to the question at: unable to read file from ftp in android?

I tried a simple:

  StringBuilder response = new StringBuilder();
  URLConnection ftpConn;
  try {
  URL netUrl = new URL("ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KABQ.TXT");
  ftpConn = netUrl.openConnection();
  BufferedInputStream bufRd = new BufferedInputStream(ftpConn.getInputStream());
  int temp;
  while ((temp = bufRd.read()) != -1) {
      response.append(temp);
  }
  bufRd.close();
  } catch (Exception e) {
      return "Failure";
  }

but it gets an exception on getInputStream: Unable to connect to server: Unable to configure data port

Also, there must be a more intelligent way to pull the data out of the stream buffer than byte-by-byte, isn't there? I can't find that either.

Lastly, I need to do both http and ftp access, is there any reason not to use URLConnection for both access types? or is it better to use HttpConnection for http and URLConnection for ftp?

Thanks!

Conform answered 13/8, 2011 at 21:51 Comment(4)
Do you have an internet uses permission in your manifest?Henni
Yes, I spent a couple of hours on that one, that would have been a good guess several hours ago. :-) I did get http to work but not ftp.Conform
I also noticed if I type that URL into google on the emulator, it says "Web page not available" and it's there in a normal browser. If I type that URL directly into the browser window, it seems to take only 50 characters, won't take the whole thing. Are these bugs in the emulator?Conform
Please help! Or else if ftp doesn't work on the Eclipse emulator, please let me know that too and suggestions for a workaround. And does it work on the real hardware?Conform
C
10

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");
Conform answered 17/8, 2011 at 0:12 Comment(2)
Looks like I can combine the directory path and file in the same call: InputStream inStream = mFTPClient.retrieveFileStream("/data/forecasts/taf/stations/KABQ.TXT");Conform
I am trying to do the exact same thing as you, just with a different station. Can we see your code?Hendrickson
C
2

And I also found on the web someplace an answer to the 'byte-by-byte' question. This seems to work to convert an InputStream type directly to String type:

      String theStr = new Scanner(inStream).useDelimiter("\\A").next();
Conform answered 17/8, 2011 at 14:4 Comment(0)
B
1

I also looked for a simple ftp download example without using of 3rd party libs. Didn't find any, so post my solution here.

URLConnection by default uses user name 'anonymous' with empty password which is not accepted by many ftp servers, as they require e-mail as the password for 'anonymous'.

To use the following code in your app, just add try..catch and make sure that reading from stream isn't block UI thread.

URL url = new URL("ftp://ftp.mozilla.org/README");
URLConnection cn = url.openConnection();
cn.setRequestProperty ("Authorization", "Basic " + Base64.encodeToString("anonymous:[email protected]".getBytes(), Base64.DEFAULT));

final File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
FileOutputStream fos = new FileOutputStream(dir.getPath() + "/README");

InputStream is = cn.getInputStream();
int bytesRead = -1;
byte[] buf = new byte[8096];
while ((bytesRead = is.read(buf)) != -1) {
    fos.write(buf, 0, bytesRead);
}
if(is != null)is.close();
if(fos != null){ fos.flush(); fos.close(); }

Hope this will save you some time.

Bobinette answered 9/2, 2014 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.