How to list ftp directories with android?

后端 未结 2 584
太阳男子
太阳男子 2021-01-03 05:15

[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 the directory list:



        
相关标签:
2条回答
  • 2021-01-03 05:49

    You can use CkFtp2 API to easily get the FTP directory listing information. Like the following:

    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";
            }
    
        }
    
    0 讨论(0)
  • 2021-01-03 06:12

    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());
                        }
    
    0 讨论(0)
提交回复
热议问题