【问题标题】:Java Current Directory Returns `null`Java 当前目录返回 `null`
【发布时间】:2015-11-19 13:50:08
【问题描述】:

我正在尝试使用来自 Apache Commons FTP 的 printWorkingDirectory(),但它只返回 null。我无法导航目录、列出文件等。

登录成功,但无论我怎么尝试,我都无法更改当前目录。 我使用以下代码:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;




public class FTPDownloadFileDemo {

    public static void main(String[] args) {
        String server = "FTP server Address";
    int port = portNo;
    String user = "User Name";
    String pass = "Pasword";
    FTPClient ftpClient = new FTPClient();
    String dir = "stocks/";
    try {

        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        System.out.println( ftpClient.printWorkingDirectory());//Always null
        //change current directory
        ftpClient.changeWorkingDirectory(dir);
        boolean success = ftpClient.changeWorkingDirectory(dir);
       // showServerReply(ftpClient);
        if (success)// never success
            System.out.println("Successfully changed working directory.");

System.out.println(ftpClient.printWorkingDirectory());// Always null


            } catch (IOException ex) {
                System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

【问题讨论】:

  • 如果你尝试这个会发生什么:String dir = "./stocks/";
  • 我已经尝试过了,但没有任何变化。
  • . *Returns: The pathname of the current working directory. If it cannot be obtained, returns null.* 2. You are ignoring the boolean returned by login()`.

标签: java apache ftp directory


【解决方案1】:

这是一个相当古老的问题,值得回答。此问题可能是在需要安全连接时使用 FTPClient 造成的。如果确实如此,您可能必须切换到 FTPSClient。此外,如果安全客户端无法解决问题,请使用以下代码 sn-p 输出来自服务器的响应以解决问题:

ftpClient.addProtocolCommandListener(
                new PrintCommandListener(
                        new PrintWriter(new OutputStreamWriter(System.out, "UTF-8")), true));

此外,如果您的 IP 地址未在白名单中,服务器可能会拒绝您的登录尝试。因此,能够查看日志势在必行。打印当前工作目录时看到 null 的原因是因为您没有登录。登录方法不会抛出异常,而是返回一个布尔值,指示操作是否成功。您在更改目录时检查是否成功,但在登录时没有这样做。

boolean success = ftpClient.login(user, pass);

【讨论】:

  • 或者登录失败是其他原因。
【解决方案2】:

我遇到了同样的情况,但我遇到了一个简单的步骤。 刚刚添加了这个。

boolean success = ftpClient.changeWorkingDirectory(dir);
ftpClient.printWorkingDirectory(); //add this line after changing the working directory
System.out.println(ftpClient.printWorkingDirectory()); //wont be getting null

这里有代码和控制台输出

FTPClient.changeWorkingDirectory - Unknown parser type: "/Path" is current directory

我知道我回复得太早了;-P,但我最近看到了这篇文章。希望这对未来的搜索者有所帮助;-)

【讨论】:

    猜你喜欢
    • 2016-09-27
    • 2012-08-08
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多