开发者

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

开发者 https://www.devze.com 2023-01-05 21:47 出处:网络
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?

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 log开发者_开发知识库in
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?


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.


  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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消