16

Is there readily available functionality for Java to create a folder hierarchy on a remote FTP server. Apache Commons does provide an FTP client, but I can't find a method for creating a directory hierarchy. It does allow for creating a single directory (makeDirectory), but creating an entire path does not seem to be in there. The reason I want this is because sometimes part of a directory hierarchy is not (yet) available and in such a case I want to create the missing part of the hierarchy and then change to that newly created directory.

Pieter
  • 163
  • 1
  • 1
  • 4

5 Answers5

29

Needed the answer to this and so I implemented and tested some code to create directories as needed. Hope this helps someone. cheers! Aaron

/**
* utility to create an arbitrary directory hierarchy on the remote ftp server 
* @param client
* @param dirTree  the directory tree only delimited with / chars.  No file name!
* @throws Exception
*/
private static void ftpCreateDirectoryTree( FTPClient client, String dirTree ) throws IOException {

  boolean dirExists = true;

  //tokenize the string and attempt to change into each directory level.  If you cannot, then start creating.
  String[] directories = dirTree.split("/");
  for (String dir : directories ) {
    if (!dir.isEmpty() ) {
      if (dirExists) {
        dirExists = client.changeWorkingDirectory(dir);
      }
      if (!dirExists) {
        if (!client.makeDirectory(dir)) {
          throw new IOException("Unable to create remote directory '" + dir + "'.  error='" + client.getReplyString()+"'");
        }
        if (!client.changeWorkingDirectory(dir)) {
          throw new IOException("Unable to change into newly created remote directory '" + dir + "'.  error='" + client.getReplyString()+"'");
        }
      }
    }
  }     
}
aaron.spear
  • 416
  • 5
  • 4
  • Should the method not iterate the hierarchy in reverse? If this method is called multiple times it must always traverse the complete tree. – djmj Jul 05 '14 at 01:10
  • 1
    I did add a client.changeWorkingDirectly("/"); call at the end of it to return the working dir to the root, for subsquent calls to the function. – user3312154 Jan 23 '17 at 20:35
22

You have to use a combination of FTPClient.changeWorkingDirectory to figure out if the directory exists, then FTPClient.makeDirectory if the call to FTPClient.changeWorkingDirectory returns false.

You need to recursively walk the directory tree in the manner described above at each level create the directory as required.

Tendayi Mawushe
  • 25,562
  • 6
  • 51
  • 57
  • Indeed that's what I figured. Basically that's creating your own version of mkdirs, but for FTP. – Pieter Nov 03 '10 at 12:33
2

Why can't you use the FTPClient#makeDirectory() method to build the hierarchy, one folder at a time?

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
2

Apache Commons VFS (Virtual File System) can access several different filesystems (FTP among them), and it also provides a createFolder method that is able to create parent directories if needed:

http://commons.apache.org/vfs/apidocs/org/apache/commons/vfs/FileObject.html#createFolder%28%29

Documentation states that method "creates this folder, if it does not exist. Also creates any ancestor folders which do not exist. This method does nothing if the folder already exists."

This may suit your needs.

jjmontes
  • 24,679
  • 4
  • 39
  • 51
  • Yeah, I read about that. Unfortunately I can't use this library as it is not available on the target system. – Pieter Nov 03 '10 at 12:34
1

Use ftpSession.mkdir function to create directory.

@ManagedOperation
private void ftpMakeDirectory(FtpSession ftpSession, String fullDirFilePath) throws IOException {
if (!ftpSession.exists(fullDirFilePath)) {
  String[] allPathDirectories = fullDirFilePath.split("/");
  StringBuilder partialDirPath = new StringBuilder("");
  for (String eachDir : allPathDirectories) {
    partialDirPath.append("/").append(eachDir);

    ftpSession.mkdir(partialDirPath.toString());
  }

}