Search code examples
javaftpdirectoryftp-clientapache-commons-net

How to create a directory with multiple levels in one call in Java using FTP


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?


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.