Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java Apache Commons网络库检索所有子文件夹内容_Java_Ftp_Ftp Client_Apache Commons Net - Fatal编程技术网

使用Java Apache Commons网络库检索所有子文件夹内容

使用Java Apache Commons网络库检索所有子文件夹内容,java,ftp,ftp-client,apache-commons-net,Java,Ftp,Ftp Client,Apache Commons Net,使用Java Apache Commons NetFTPClient,是否可以进行listFiles调用,以检索目录及其所有子目录的内容?库无法自行执行此操作。但您可以使用简单的递归实现它: private static void listFolder(FTPClient ftpClient, String remotePath) throws IOException { System.out.println("Listing folder " + remot

使用Java Apache Commons Net
FTPClient
,是否可以进行
listFiles
调用,以检索目录及其所有子目录的内容?

库无法自行执行此操作。但您可以使用简单的递归实现它:

private static void listFolder(FTPClient ftpClient, String remotePath)
    throws IOException
{
    System.out.println("Listing folder " + remotePath);
    FTPFile[] remoteFiles = ftpClient.listFiles(remotePath);
    for (FTPFile remoteFile : remoteFiles)
    {
        if (!remoteFile.getName().equals(".") && !remoteFile.getName().equals(".."))
        {
            String remoteFilePath = remotePath + "/" + remoteFile.getName();

            if (remoteFile.isDirectory())
            {
                listFolder(ftpClient, remoteFilePath);
            }
            else
            {
                System.out.println("Found file " + remoteFilePath);
            }
        }
    }
}

不仅如此,ApacheCommonsNet库不能在一次调用中实现这一点。FTP中实际上没有用于此目的的API。虽然有些FTP服务器有专有的非标准方式。例如,ProFTPD有
-R
切换到
列表
命令(及其伙伴)

另请参见相关的C#问题:


您还可以考虑通过java从SSH运行代码> DU-SH*./COD>。
FTPFile[] remoteFiles = ftpClient.listFiles("-R " + remotePath);