英文:
org.apache.commons.net.ftp.FTPClient listFiles always returns files from root directory irrespective of the pathname given in argument
问题
在FTPClient类中,listFiles方法始终返回根目录中的目录或文件,即使指定了路径作为参数。
public static void main(String[] args) {
String server = "192.168.0.60";
int port = 21;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.enterLocalPassiveMode();
ftpClient.login("anonymous", "");
FTPFile[] files = ftpClient.listFiles("/StorageCard");
for (FTPFile ftpFile : files) {
System.out.println(ftpFile.getName());
}
} catch (IOException io) {
io.printStackTrace();
}
}
StorageCard是根文件夹内的一个目录。
我得到的输出是:
1. Network Internal Storage
2. WinDrive
3. StorageCard
4. Application Data
5. My Documents
6. Program Files
7. Windows
8. .........
<details>
<summary>英文:</summary>
listFiles in the class FTPClient is always returning the directories or files from the root directory, even when the path is given as argument.
public static void main(String[] args) {
String server = "192.168.0.60";
int port = 21;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server,port);
ftpClient.enterLocalPassiveMode();
ftpClient.login("anonymous", "");
FTPFile[] files = ftpClient.listFiles("/StorageCard");
for(FTPFile ftpFile: files) {
System.out.println(ftpFile.getName());
}
} catch(IOException io) {
io.printStackTrace();
}
}
StorageCard is a directory inside the rootfolder
Output what I get is
1. Network Internal Storage
2. WinDrive
3. **StorageCard**
4. Application Data
5. My Documents
6. Program Files
7. Windows
8. .........
</details>
# 答案1
**得分**: 0
你应该在使用 `listFiles()` 之前更改工作目录。
```java
ftpClient.changeWorkingDirectory("StorageCard");
FTPFile[] files = ftpClient.listFiles();
英文:
You should be changing working directory before using listFiles()
ftpClient.changeWorkingDirectory("StorageCard");
FTPFile[] files = ftpClient.listFiles();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论