How to list ftp directories using android?

[SOLVED] How can I get a list of my files and folders on my ftp server?

I know how to connect and upload a file, but not how to get a list of directories:

try { FTPClient ftpClient = new FTPClient(); ftpClient.connect(InetAddress.getByName("176.28.25.46")); ftpClient.login("******", "******"); System.out.println("status :: " + ftpClient.getStatus()); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); // Prepare file to be uploaded to FTP Server File file = new File("default.prop"); FileInputStream ifile = new FileInputStream(file); // Upload file to FTP Server ftpClient.storeFile("/subdomains/giveyourapps/httpdocs/apps/default.prop",ifile); ftpClient.disconnect(); } catch (Exception e) { e.printStackTrace(); } 

But every piece of code I found on google didn't work for me: - /

  try { FTPClient ftpClient = new FTPClient(); ftpClient.connect(InetAddress.getByName("176.28.25.46")); ftpClient.login("******", "******"); System.out.println("status :: " + ftpClient.getStatus()); String toppath = new String(); FTPFile[] ftpDirs = ftpClient.listDirectories(); for (int i = 0; i < ftpDirs.length; i++) { toppath = ftpDirs[0].getName(); Log.d("CONNECT", "Directories in the ftp server are " + ftpDirs[i].getName()); } FTPFile[] ftpdirs2 = ftpClient.listFiles(toppath); for (int i = 0; i < ftpdirs2.length; i++) { Log.d("CONNECT", "File i need is " + ftpdirs2[i].getName()); } } 

For everyone who has the same problem. Now it works with this code: (Thanks to user 1106888)

  try { FTPClient ftpClient = new FTPClient(); ftpClient.connect(InetAddress.getByName("176.28.25.46")); ftpClient.login("******", "******"); System.out.println("status :: " + ftpClient.getStatus()); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); try{ String toppath = new String(); FTPFile[] ftpDirs = ftpClient.listDirectories(); for (int i = 0; i < ftpDirs.length; i++) { toppath = ftpDirs[0].getName(); System.out.println("Directory->: " + ftpDirs[i].getName()); } FTPFile[] ftpdirs2 = ftpClient.listFiles(toppath); for (int i = 0; i < ftpdirs2.length; i++) { System.out.println("Files->: " + ftpdirs2[i].getName()); } }catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } 
+4
source share
2 answers

Use this code and it should help

 FTPFile[] ftpDirs = mFTPClient.listDirectories(); for (int i = 0; i < ftpDirs.length; i++) { toppath = ftpDirs[0].getName(); Log.d("CONNECT", "Directories in the ftp server are " + ftpDirs[i].getName()); } FTPFile[] ftpdirs2 = mFTPClient.listFiles(toppath); for (int i = 0; i < ftpdirs2.length; i++) { Log.d("CONNECT", "File i need is " + ftpdirs2[i].getName()); } 
+1
source

You can use the CkFtp2 API to easily retrieve FTP directory listing information. As below:

 CkFtp2 ftp = new CkFtp2(); int n = ftp.get_NumFilesAndDirs(); if (n < 0) { outStr += ftp.lastErrorText() + "\n"; tv.setText(outStr); setContentView(tv); return; } if (n > 0) { for (int i = 0; i <= n - 1; i++) { // Display the filename outStr += ftp.getFilename(i) + "\n"; // Display the file size (in bytes) outStr += ftp.GetSize(i) + "\n"; // Is this a sub-directory? if (ftp.GetIsDirectory(i) == true) { outStr += ".. this is a sub-directory" + "\n"; } outStr += "--" + "\n"; } } 
+1
source

Source: https://habr.com/ru/post/1443472/


All Articles