user2735306
user2735306

Reputation: 13

Java Current Directory Returns `null`

I'm trying to use the printWorkingDirectory() from Apache Commons FTP but it's only returning null. I can't navigate directories, list files, etc.

Log in pass all is success but how ever I try I can not change current directory. I use this following code:

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();
            }
        }
    }
}

Upvotes: 1

Views: 1316

Answers (2)

S14321K
S14321K

Reputation: 230

I faced the same, but I came across with a simple step. Just added this.

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

Here I have the code and the console output

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

I know I replied too soon ;-P, but I saw this post recently. Hope this helps to future searchers ;-)

Upvotes: 0

Dennis
Dennis

Reputation: 346

This is rather old question that deserves an answer. This issue is likely a result of using FTPClient when secure connection is required. You may have to switch to FTPSClient if that is, indeed, the case. Further, output the response from the server with the following code snippet to troubleshoot the issue if secure client doesn't solve the it:

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

Also, a server can reject your login attempt if your IP address is not white listed. So, being able to see the logs is imperative. The reason you see null when printing current working directory is because you are not logged in. Login method will not throw an exception but rather return a boolean value indicating if the operation succeeded. You are checking for success when changing a directory but not doing so when logging in.

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

Upvotes: 1

Related Questions