Example usage for org.apache.commons.net.ftp FTPClient makeDirectory

List of usage examples for org.apache.commons.net.ftp FTPClient makeDirectory

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPClient makeDirectory.

Prototype

public boolean makeDirectory(String pathname) throws IOException 

Source Link

Document

Creates a new subdirectory on the FTP server in the current directory (if a relative pathname is given) or where specified (if an absolute pathname is given).

Usage

From source file:org.apache.tools.ant.taskdefs.optional.net.FTP.java

/**
 * Create the specified directory on the remote host.
 *
 * @param ftp The FTP client connection/*from   w w  w . ja v  a 2 s.c om*/
 * @param dir The directory to create (format must be correct for host
 *      type)
 * @throws IOException  in unknown circumstances
 * @throws BuildException if ignoreNoncriticalErrors has not been set to true
 *         and a directory could not be created, for instance because it was
 *         already existing. Precisely, the codes 521, 550 and 553 will trigger
 *         a BuildException
 */
protected void makeRemoteDir(FTPClient ftp, String dir) throws IOException, BuildException {
    String workingDirectory = ftp.printWorkingDirectory();
    if (verbose) {
        if (dir.startsWith("/") || workingDirectory == null) {
            log("Creating directory: " + dir + " in /");
        } else {
            log("Creating directory: " + dir + " in " + workingDirectory);
        }
    }
    if (dir.startsWith("/")) {
        ftp.changeWorkingDirectory("/");
    }
    String subdir = "";
    StringTokenizer st = new StringTokenizer(dir, "/");
    while (st.hasMoreTokens()) {
        subdir = st.nextToken();
        log("Checking " + subdir, Project.MSG_DEBUG);
        if (!ftp.changeWorkingDirectory(subdir)) {
            if (!ftp.makeDirectory(subdir)) {
                // codes 521, 550 and 553 can be produced by FTP Servers
                //  to indicate that an attempt to create a directory has
                //  failed because the directory already exists.
                int rc = ftp.getReplyCode();
                if (!(ignoreNoncriticalErrors && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) {
                    throw new BuildException("could not create directory: " + ftp.getReplyString());
                }
                if (verbose) {
                    log("Directory already exists");
                }
            } else {
                if (verbose) {
                    log("Directory created OK");
                }
                ftp.changeWorkingDirectory(subdir);
            }
        }
    }
    if (workingDirectory != null) {
        ftp.changeWorkingDirectory(workingDirectory);
    }
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * Creates all parent directories specified in a complete relative
 * pathname. Attempts to create existing directories will not cause
 * errors.//from www  .j  a v  a 2 s.  com
 *
 * @param ftp the FTP client instance to use to execute FTP actions on
 *        the remote server.
 * @param filename the name of the file whose parents should be created.
 * @throws IOException under non documented circumstances
 * @throws BuildException if it is impossible to cd to a remote directory
 *
 */
protected void createParents(FTPClient ftp, String filename) throws IOException, BuildException {

    File dir = new File(filename);
    if (dirCache.contains(dir)) {
        return;
    }

    Vector parents = new Vector();
    String dirname;

    while ((dirname = dir.getParent()) != null) {
        File checkDir = new File(dirname);
        if (dirCache.contains(checkDir)) {
            break;
        }
        dir = checkDir;
        parents.addElement(dir);
    }

    // find first non cached dir
    int i = parents.size() - 1;

    if (i >= 0) {
        String cwd = ftp.printWorkingDirectory();
        String parent = dir.getParent();
        if (parent != null) {
            if (!ftp.changeWorkingDirectory(resolveFile(parent))) {
                throw new BuildException("could not change to " + "directory: " + ftp.getReplyString());
            }
        }

        while (i >= 0) {
            dir = (File) parents.elementAt(i--);
            // check if dir exists by trying to change into it.
            if (!ftp.changeWorkingDirectory(dir.getName())) {
                // could not change to it - try to create it
                task.log("creating remote directory " + resolveFile(dir.getPath()), Project.MSG_VERBOSE);
                if (!ftp.makeDirectory(dir.getName())) {
                    handleMkDirFailure(ftp);
                }
                if (!ftp.changeWorkingDirectory(dir.getName())) {
                    throw new BuildException("could not change to " + "directory: " + ftp.getReplyString());
                }
            }
            dirCache.add(dir);
        }
        ftp.changeWorkingDirectory(cwd);
    }
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * Create the specified directory on the remote host.
 *
 * @param ftp The FTP client connection/* w ww .j a  v a  2 s .  c  om*/
 * @param dir The directory to create (format must be correct for host
 *      type)
 * @throws IOException  in unknown circumstances
 * @throws BuildException if ignoreNoncriticalErrors has not been set to true
 *         and a directory could not be created, for instance because it was
 *         already existing. Precisely, the codes 521, 550 and 553 will trigger
 *         a BuildException
 */
protected void makeRemoteDir(FTPClient ftp, String dir) throws IOException, BuildException {
    String workingDirectory = ftp.printWorkingDirectory();
    if (task.isVerbose()) {
        if (dir.startsWith("/") || workingDirectory == null) {
            task.log("Creating directory: " + dir + " in /");
        } else {
            task.log("Creating directory: " + dir + " in " + workingDirectory);
        }
    }
    if (dir.startsWith("/")) {
        ftp.changeWorkingDirectory("/");
    }
    String subdir = "";
    StringTokenizer st = new StringTokenizer(dir, "/");
    while (st.hasMoreTokens()) {
        subdir = st.nextToken();
        task.log("Checking " + subdir, Project.MSG_DEBUG);
        if (!ftp.changeWorkingDirectory(subdir)) {
            if (!ftp.makeDirectory(subdir)) {
                // codes 521, 550 and 553 can be produced by FTP Servers
                //  to indicate that an attempt to create a directory has
                //  failed because the directory already exists.
                int rc = ftp.getReplyCode();
                if (!(task.isIgnoreNoncriticalErrors()
                        && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) {
                    throw new BuildException("could not create directory: " + ftp.getReplyString());
                }
                if (task.isVerbose()) {
                    task.log("Directory already exists");
                }
            } else {
                if (task.isVerbose()) {
                    task.log("Directory created OK");
                }
                ftp.changeWorkingDirectory(subdir);
            }
        }
    }
    if (workingDirectory != null) {
        ftp.changeWorkingDirectory(workingDirectory);
    }
}

From source file:org.jboss.ejb3.examples.ch06.filetransfer.FileTransferBean.java

@Override
public void mkdir(final String directory) {
    // Get the client
    final FTPClient client = this.getClient();

    // Exec cd//from  www  .j  a  v a  2 s. c om
    try {
        // Exec mkdir
        client.makeDirectory(directory);

        // Check reply for success
        this.checkLastOperation();
    } catch (final Exception e) {
        throw new FileTransferException("Could not make directory \"" + directory + "\"", e);
    }

}

From source file:org.moxie.ftp.FTP.java

/**
 * Create the specified directory on the remote host.
 *
 * @param ftp The FTP client connection//from   ww  w .java  2s.  c om
 * @param dir The directory to create (format must be correct for host
 *      type)
 * @throws IOException  in unknown circumstances
 * @throws BuildException if ignoreNoncriticalErrors has not been set to true
 *         and a directory could not be created, for instance because it was
 *         already existing. Precisely, the codes 521, 550 and 553 will trigger
 *         a BuildException
 */
protected void makeRemoteDir(FTPClient ftp, String dir) throws IOException, BuildException {
    String workingDirectory = ftp.printWorkingDirectory();
    if (verbose) {
        if (dir.indexOf("/") == 0 || workingDirectory == null) {
            log("Creating directory: " + dir + " in /");
        } else {
            log("Creating directory: " + dir + " in " + workingDirectory);
        }
    }
    if (dir.indexOf("/") == 0) {
        ftp.changeWorkingDirectory("/");
    }
    String subdir = "";
    StringTokenizer st = new StringTokenizer(dir, "/");
    while (st.hasMoreTokens()) {
        subdir = st.nextToken();
        log("Checking " + subdir, Project.MSG_DEBUG);
        if (!ftp.changeWorkingDirectory(subdir)) {
            if (!ftp.makeDirectory(subdir)) {
                // codes 521, 550 and 553 can be produced by FTP Servers
                //  to indicate that an attempt to create a directory has
                //  failed because the directory already exists.
                int rc = ftp.getReplyCode();
                if (!(ignoreNoncriticalErrors && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) {
                    throw new BuildException("could not create directory: " + ftp.getReplyString());
                }
                if (verbose) {
                    log("Directory already exists");
                }
            } else {
                if (verbose) {
                    log("Directory created OK");
                }
                ftp.changeWorkingDirectory(subdir);
            }
        }
    }
    if (workingDirectory != null) {
        ftp.changeWorkingDirectory(workingDirectory);
    }
}

From source file:org.moxie.ftp.FTPTaskMirrorImpl.java

/**
 * Create the specified directory on the remote host.
 *
 * @param ftp The FTP client connection//from w ww. j av a 2s.  com
 * @param dir The directory to create (format must be correct for host
 *      type)
 * @throws IOException  in unknown circumstances
 * @throws BuildException if ignoreNoncriticalErrors has not been set to true
 *         and a directory could not be created, for instance because it was
 *         already existing. Precisely, the codes 521, 550 and 553 will trigger
 *         a BuildException
 */
protected void makeRemoteDir(FTPClient ftp, String dir) throws IOException, BuildException {
    String workingDirectory = ftp.printWorkingDirectory();
    if (task.isVerbose()) {
        if (dir.indexOf("/") == 0 || workingDirectory == null) {
            task.log("Creating directory: " + dir + " in /");
        } else {
            task.log("Creating directory: " + dir + " in " + workingDirectory);
        }
    }
    if (dir.indexOf("/") == 0) {
        ftp.changeWorkingDirectory("/");
    }
    String subdir = "";
    StringTokenizer st = new StringTokenizer(dir, "/");
    while (st.hasMoreTokens()) {
        subdir = st.nextToken();
        task.log("Checking " + subdir, Project.MSG_DEBUG);
        if (!ftp.changeWorkingDirectory(subdir)) {
            if (!ftp.makeDirectory(subdir)) {
                // codes 521, 550 and 553 can be produced by FTP Servers
                //  to indicate that an attempt to create a directory has
                //  failed because the directory already exists.
                int rc = ftp.getReplyCode();
                if (!(task.isIgnoreNoncriticalErrors()
                        && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) {
                    throw new BuildException("could not create directory: " + ftp.getReplyString());
                }
                if (task.isVerbose()) {
                    task.log("Directory already exists");
                }
            } else {
                if (task.isVerbose()) {
                    task.log("Directory created OK");
                }
                ftp.changeWorkingDirectory(subdir);
            }
        }
    }
    if (workingDirectory != null) {
        ftp.changeWorkingDirectory(workingDirectory);
    }
}

From source file:org.ow2.proactive.scheduler.examples.FTPConnector.java

private void createRemoteDirectory(FTPClient ftpClient, String remoteDirPath) throws IOException {
    if (ftpClient.makeDirectory(remoteDirPath)) {
        getOut().println("CREATED the directory: " + remoteDirPath);
    } else {/*from   w ww  .  ja  va 2 s . c om*/
        throw new IOException("Error: COULD NOT create the directory: " + remoteDirPath);
    }
}

From source file:org.shept.util.FtpFileCopy.java

/**
 * Creates the needed directories if necessary.
 * @param ftpClient A <code>FTPClient</code> being <b>connected</b>.
 * @param basePath The base path. This one <b>has to exist</b> on the ftp server!
 * @param path The path to be created.//from  www  . j  a va2  s  .c om
 * @throws IOException IN case of an error.
 */
private boolean ensureFtpDirectory(FTPClient ftpClient, String basePath, String path) throws IOException {
    ftpClient.changeWorkingDirectory(basePath);
    StringTokenizer tokenizer = new StringTokenizer(path, "/");
    while (tokenizer.hasMoreTokens()) {
        String folder = tokenizer.nextToken();
        FTPFile[] ftpFile = ftpClient.listFiles(folder);
        if (ftpFile.length == 0) {
            // create the directoy
            if (!ftpClient.makeDirectory(folder)) {
                logger.error(
                        "Ftp Creating the destination directory did not succeed " + ftpClient.getReplyString());
                return false;
            }
        }
        ftpClient.changeWorkingDirectory(folder);
    }
    return true;
}

From source file:org.spka.cursus.publish.website.ftp.PrepareDirectory.java

public void on(FTPClient ftp) throws IOException {
    if (!ftp.changeWorkingDirectory(Constants.RESULTS_DIR)) {
        if (!ftp.makeDirectory(Constants.RESULTS_DIR)) {
            throw new IllegalStateException("Unable to create results dir");
        }/*from  ww  w . j  a  va 2  s . c o m*/

        if (!ftp.changeWorkingDirectory(Constants.RESULTS_DIR)) {
            throw new IllegalStateException("Unable to change to results dir");
        }
    }

    ftp.changeWorkingDirectory("/");
}

From source file:org.structr.files.ftp.FtpDirectoriesTest.java

public void test02MkDir() {

    FTPClient ftp = setupFTPClient();

    FTPFile[] dirs = null;//from  w w w. j a v a 2s  .  c o  m

    final String name1 = "FTPdir1";
    final String name2 = "FTPdir2";
    boolean success = false;

    try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {

        assertEmptyDirectory(ftp);

        // Create folder by mkdir FTP command
        success = ftp.makeDirectory(name1);
        assertTrue(success);

        tx.success();

    } catch (IOException | FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex.getMessage());
    }

    try (final Tx tx = app.tx()) {

        dirs = ftp.listDirectories();

        assertNotNull(dirs);
        assertEquals(1, dirs.length);
        assertEquals(name1, dirs[0].getName());

        // Create second folder in /
        success = ftp.makeDirectory(name2);
        assertTrue(success);

        tx.success();

    } catch (IOException | FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex.getMessage());
    }

    try (final Tx tx = app.tx()) {

        dirs = ftp.listDirectories();

        assertNotNull(dirs);
        assertEquals(2, dirs.length);
        assertEquals(name1, dirs[0].getName());
        assertEquals(name2, dirs[1].getName());

        ftp.disconnect();

        tx.success();

    } catch (IOException | FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex.getMessage());
    }
}