RapsFan1981
RapsFan1981

Reputation: 1197

Why is printWorkingDirectory() returning only the root dir?

I'm trying to use the printWorkingDirectory() from Apache Commons FTP but it's only returning "/" the root dir regardless of what directory I'm actually in. I can navigate directories, list files etc. When I upload a file it is uploaded to the ftp it is uploaded to the root regardless of what directory I'm in when the file is uploaded. When I long click a local file it triggers an Upload Dialog.

    public boolean ftpConnect(String host, String username, String password,
            int port) {
        try {
            mFTPClient = new FTPClient();
            // connecting to the host
            mFTPClient.connect(host, port);

            // now check the reply code, if positive mean connection success
            if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                // login using username & password
                boolean status = mFTPClient.login(username, password);

                /*
                 * Set File Transfer Mode
                 * 
                 * To avoid corruption issue you must specified a correct
                 * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
                 * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE for
                 * transferring text, image, and compressed files.
                 */
                mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
                mFTPClient.enterLocalPassiveMode();

                ftpPrintFilesList(ftpGetCurrentWorkingDirectory());
                return status;
            }
        } catch (Exception e) {
            // Log.d(TAG, "Error: could not connect to host " + host );
        }

        return false;
    }

    public boolean ftpDisconnect() {
        try {
            mFTPClient.logout();
            mFTPClient.disconnect();
            return true;
        } catch (Exception e) {
            // Log.d(TAG,
            // "Error occurred while disconnecting from ftp server.");
        }

        return false;
    }

    public String ftpGetCurrentWorkingDirectory() {
        try {
            String workingDir = mFTPClient.printWorkingDirectory(); 
            return workingDir;
        } catch (Exception e) {
            Log.i("Error working dir", e.toString());
        }

        return null;
    }

    public boolean ftpChangeDirectory(String directory_path) {
        try {
            if (mFTPClient.changeWorkingDirectory(directory_path))
                return true;
        } catch (Exception e) {
            Log.i("directory path", directory_path);
        }

        return false;
    }

    public boolean ftpUpload(String srcFilePath, String desFileName,
            String desDirectory) {
        boolean status = false;
        try {
            FileInputStream srcFileStream = new FileInputStream(srcFilePath);

            // change working directory to the destination directory
            if (ftpChangeDirectory(desDirectory)) {
                status = mFTPClient.storeFile(desFileName, srcFileStream);
            }
            showServerReply(mFTPClient);
            srcFileStream.close();
            return status;
        } catch (Exception e) {
            Log.i("Error", e.toString());
        }

        return status;
    }

    public void l_Show_Alert_box(Context context, String message, int position) {
        final String selectedFileString = l_directoryEntries.get(position)
                .getText();
        final File tmpFile = new File(selectedFileString);
        final String fname = tmpFile.getName();
        String btn;
        final AlertDialog alertDialog = new AlertDialog.Builder(context)
                .create();

        if (tmpFile.isDirectory()) {
            btn = "Make Directory";
            alertDialog.setIcon(R.drawable.newfolder);
            alertDialog.setTitle(getString(R.string.mkdir));

            alertDialog.setButton(btn, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    ftpMakeDirectory("/" + fname);
                    map.clear();
                    r_directoryEntries.clear();
                    r_itla.notifyDataSetChanged();
                    ftpPrintFilesList(ftpGetCurrentWorkingDirectory());
                    return;
                }

            });
        } else {
            btn = "Upload";
            alertDialog.setIcon(R.drawable.upload);
            alertDialog.setTitle(getString(R.string.upload));
            alertDialog.setButton(btn, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    String cd;
                    try {
                        cd = mFTPClient.printWorkingDirectory().toString(); // Debug shows the working dir as being "/"
                        //ftpUpload(currenLocalFile.getPath(), "test.txt", cd);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    map.clear();
                    r_directoryEntries.clear();
                    r_itla.notifyDataSetChanged();
                    ftpPrintFilesList(ftpGetCurrentWorkingDirectory());
                    return;
                }

            });
        }
        alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            }
        });

        alertDialog.setMessage(message);
        alertDialog.show();
    }


}

Why is printWorkingDirectory() not returning the correct directory?

Upvotes: 0

Views: 1614

Answers (2)

TactMayers
TactMayers

Reputation: 884

You need to call changeWorkingDirectory(String pathname) method to change the working directory, otherwise it will stay in root.

Upvotes: 1

MarsAtomic
MarsAtomic

Reputation: 10696

Q: "Why is printWorkingDirectory() not returning the correct directory?"

A: Because it actually is returning the correct directory. It's your expectation that's flawed.

You need to make a file object that points where you want your work to take place. You can use .getPath() or getName(), etc. to then get the string that you want.

File workingDir = new File("/do/my/work/here");

Take a look at the official doc here

Upvotes: 1

Related Questions