ftpClient获取目录下面文件夹
时间: 2023-08-03 10:04:29 浏览: 287
可以使用FTPClient的listFiles()方法来获取目录下的所有文件和文件夹,然后筛选出文件夹即可。以下是一个示例代码:
```java
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FTPDemo {
public static void main(String[] args) {
String server = "your-ftp-server.com";
int port = 21;
String user = "your-ftp-username";
String password = "your-ftp-password";
String remotePath = "/your/remote/folder/path";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, password);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
FTPFile[] files = ftpClient.listFiles(remotePath);
for (FTPFile file : files) {
if (file.isDirectory()) {
System.out.println("Found directory: " + file.getName());
}
}
ftpClient.logout();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
```
其中,remotePath为远程目录的路径。如果要获取根目录下的文件夹,则remotePath应该是"/"。
阅读全文