0

I am using FTPClient for creating a sub directory in the FTP server. I want to create a directory "Archive " under "Root".I use the following function for creating the sub directory.

boolean s = ftNew.makeDirectory("/"+folderName+"/Archive"); 

But it returns false and cannot create sub directory "Archive". How to solve this?

Community
  • 1
  • 1
Gapchoos
  • 1,422
  • 5
  • 20
  • 40

2 Answers2

2
  1. User may not have permission to create directory (in this particular place at least).
  2. Does directory described by "folderName" exist? If not then you'll have some difficulties creating entire directory hierarchy (i.e. /{folderName}/Archive) in single call. Ensure "folderName" exists or create it in separate step.

FTPClient's makeDirectory returns true or false, it's not very usefull and rather ambiguous result. Fortunatelly you can improve your code to report precise FTP status messages.

Here's what you need:

private static void showServerReply(FTPClient ftpClient) {
    String[] replies = ftpClient.getReplyStrings();
    if (replies != null && replies.length > 0) {
        for (String aReply : replies) {
            System.out.println("SERVER: " + aReply);
        }
    }
}

Call this after every FTPClient method, for example:

package apachenet.ftp;

import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;

public class App {
    public static void main( String[] args ) {
        FTPClient client = new FTPClient(); 
        FileInputStream fis = null;
        try {
            client.connect("127.0.0.1");
            showServerReply(client);
            client.login("pwyrwinski", "secret");
            showServerReply(client);
            System.out.println("Current working directory is: " + client.printWorkingDirectory());
            String someDirectory = "nonexistentDir";
            client.makeDirectory("/" + someDirectory + "/Archive");
            showServerReply(client);

            client.logout();
            showServerReply(client);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void showServerReply(FTPClient ftpClient) {
        // ... 
    }
}

Result on my machine:

SERVER: 220 (vsFTPd 2.3.5)
SERVER: 230 Login successful.
Current working directory is: "/home/pwyrwinski"
SERVER: 550 Create directory operation failed.
SERVER: 221 Goodbye.

And when I changed String someDirectory to "home/pwyrwinski" :

SERVER: 220 (vsFTPd 2.3.5)
SERVER: 230 Login successful.
Current working directory is: "/home/pwyrwinski"
SERVER: 257 "/home/pwyrwinski/Archive" created
SERVER: 221 Goodbye.

550 is code for permission or access denied, this and other codes are easy to google.
I hope this will help you.

Paweł Wyrwiński
  • 1,393
  • 9
  • 13
0

you should create the directory step by step.otherwise,you will get create directory false.There is a example for you. http://www.codejava.net/java-se/networking/ftp/creating-nested-directory-structure-on-a-ftp-server

martin
  • 1
  • 1