How to make simple ftp get file on Android

I cannot find an example of simple FTP access to a file anywhere, and the FTPClient class (which is used in several examples) does not appear in the Android class index. I have http access, but how do I make simple FTP access? All I want to do is download (for example): ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KABQ.TXT It should not contain a login, change directory, etc. Just giving this URL to http access methods does not work.

This seems like a question: could not read the file with ftp in android?

I tried simply:

  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 in getInputStream: Unable to connect to server: cannot configure data port

, , , ? .

, HTTP, FTP-, URLConnection ? HttpConnection http URLConnection ftp?

!

+5
3

! , , . webOS WPF/#, ftp:://... FTPClient.

(Project | Properties | Java | | JARs...) , . FTPClient. , .

  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");
+8

- "". , , InputStream String:

      String theStr = new Scanner(inStream).useDelimiter("\\A").next();
+2

ftp . , .

URLConnection "" , ftp-, "".

, try..catch , .

URL url = new URL("ftp://ftp.mozilla.org/README");
URLConnection cn = url.openConnection();
cn.setRequestProperty ("Authorization", "Basic " + Base64.encodeToString("anonymous:a@b.c".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(); }

, .

+1

All Articles