Question

I am using the FTPClient library from Apache and cannot figure out a simple way to create a new directory that is more than one level deep. Am I missing something?

Assuming the directory /tmp already exists on my remote host, the following command succeeds in creating /tmp/xxx

String path = "/tmp/xxx";
FTPClient ftpc = new FTPClient();
... // establish connection and login
ftpc.makeDirectory(path);

but the following fails:

String path = "/tmp/yyy/zzz";
FTPClient ftpc = new FTPClient();
... // establish connection and login
ftpc.makeDirectory(path);

In that latter case, even /tmp/yyy isn't created.

I know I can create /tmp/yyy and then create /tmp/yyy/zzz, but I can't figure out how to create directly /tmp/yyy/zzz.

  1. Am I missing something obvious? Using mkd instead of makeDirectory didn't help.

  2. Also, is it possible in one call to upload a file to /tmp/yyy/zzz/test.txt if the directory /tmp/yyy/zzz/ doesn't exist already?

Was it helpful?

Solution

  1. FTP servers typically only allows you to create 1 level of a directory at a time. Thus you'll have to break up the path yourself, and issue one makeDirectory() call for each of the components.

  2. No.

OTHER TIPS

You need to do them one at a time, first /tmp/yyy and then /tmp/yyy/zzz. There is no short-cut mechanism for what you want to do.

The FTP protocol doesn't permit this. So no, you can't create a directory with multiple levels in one call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top