org.apache.commons.net.ftp.FTPClient listFiles always returns files from root directory irrespective of the pathname given in argument

huangapple go评论51阅读模式
英文:

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 = &quot;192.168.0.60&quot;;
        int port = 21;
        FTPClient ftpClient = new FTPClient();
        try {

            ftpClient.connect(server,port);
            ftpClient.enterLocalPassiveMode();
            ftpClient.login(&quot;anonymous&quot;, &quot;&quot;);
            FTPFile[] files = ftpClient.listFiles(&quot;/StorageCard&quot;);
            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(&quot;StorageCard&quot;);
FTPFile[] files = ftpClient.listFiles();

huangapple
  • 本文由 发表于 2020年7月22日 14:08:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63027930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定