使用Java Apache Commons网络库检索所有子文件夹内容
使用Java Apache Commons Net使用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
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);