4

[已解决] 如何获取我的 ftp 服务器上的文件和文件夹列表?

我知道如何连接和上传文件,但不知道如何获取目录列表:

            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();
            } 

但是我在谷歌上找到的每个代码都对我不起作用:-/

            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());
                }

            }

对于每个有同样问题的人。它现在可以使用该代码:(感谢 user1106888)

            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

2 回答 2

1

您可以使用CkFtp2 API 轻松获取 FTP 目录列表信息。如下所示:

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";
        }

    }
于 2012-11-01T15:52:18.803 回答
1

使用此代码,它应该会有所帮助

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());
                    }
于 2012-11-01T16:54:26.713 回答