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

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

Introduction

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

Prototype

public boolean changeToParentDirectory() throws IOException 

Source Link

Document

Change to the parent directory of the current working directory.

Usage

From source file:it.zero11.acme.example.FTPChallengeListener.java

private void deleteChallengeFiles() {
    FTPClient ftp = new FTPClient();
    try {//ww w  .  j a v a 2s. c  o  m
        ftp.connect(host);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            ftp.disconnect();
            return;
        }

        ftp.login(username, password);
        ftp.changeWorkingDirectory(webroot);
        ftp.changeWorkingDirectory(".well-known");
        ftp.changeWorkingDirectory("acme-challenge");

        FTPFile[] subFiles = ftp.listFiles();

        if (subFiles != null && subFiles.length > 0) {
            for (FTPFile aFile : subFiles) {
                String currentFileName = aFile.getName();
                if (currentFileName.equals(".") || currentFileName.equals("..")) {
                    continue;
                } else {
                    ftp.deleteFile(currentFileName);
                }
            }
        }
        ftp.changeToParentDirectory();
        ftp.removeDirectory("acme-challenge");
        ftp.changeToParentDirectory();
        ftp.removeDirectory(".well-known");
        ftp.logout();
    } catch (IOException e) {
        throw new AcmeException(e);
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:com.bdaum.zoom.net.core.ftp.FtpAccount.java

@SuppressWarnings("fallthrough")
private int transferFiles(FTPClient ftp, File[] files, IProgressMonitor monitor, IAdaptable adaptable,
        boolean deleteTransferred) throws IOException {
    if (monitor.isCanceled())
        return -1;
    FTPFile[] oldfiles = ftp.listFiles();
    if (monitor.isCanceled())
        return -1;
    Map<String, FTPFile> names = new HashMap<String, FTPFile>();
    for (FTPFile file : oldfiles)
        names.put(file.getName(), file);
    int n = 0;/*from w ww .ja v  a  2  s  .c  om*/
    for (File file : files) {
        final String filename = file.getName();
        FTPFile ftpFile = names.get(filename);
        if (file.isDirectory()) {
            if (ftpFile != null) {
                if (!ftpFile.isDirectory())
                    throw new IOException(
                            NLS.bind(Messages.FtpAccount_cannot_replace_file_with_subdir, filename));
                boolean result = ftp.changeWorkingDirectory(Core.encodeUrlSegment(filename));
                if (!result)
                    throw new IOException(NLS.bind(Messages.FtpAccount_cannot_change_to_working_dir, filename));
                // System.out.println(filename + " is new directory"); //$NON-NLS-1$
            } else {
                ftp.makeDirectory(filename);
                boolean result = ftp.changeWorkingDirectory(Core.encodeUrlSegment(filename));
                if (!result)
                    throw new IOException(Messages.FtpAccount_creation_of_subdir_failed);
                // System.out.println(filename + " is new directory"); //$NON-NLS-1$
            }
            if (monitor.isCanceled())
                return -1;
            int c = transferFiles(ftp, file.listFiles(), monitor, adaptable, deleteTransferred);
            if (c < 0)
                return -1;
            n += c;
            ftp.changeToParentDirectory();
            // System.out.println("Returned to parent directory"); //$NON-NLS-1$
        } else {
            if (ftpFile != null) {
                if (ftpFile.isDirectory())
                    throw new IOException(
                            NLS.bind(Messages.FtpAccount_cannot_replace_subdir_with_file, filename));
                if (skipAll) {
                    if (deleteTransferred)
                        file.delete();
                    continue;
                }
                if (!replaceAll) {
                    if (monitor.isCanceled())
                        return -1;
                    int ret = 4;
                    IDbErrorHandler errorHandler = Core.getCore().getErrorHandler();
                    if (errorHandler != null) {
                        String[] buttons = (filecount > 1)
                                ? new String[] { Messages.FtpAccount_overwrite,
                                        Messages.FtpAccount_overwrite_all, IDialogConstants.SKIP_LABEL,
                                        Messages.FtpAccount_skip_all, IDialogConstants.CANCEL_LABEL }
                                : new String[] { Messages.FtpAccount_overwrite, IDialogConstants.SKIP_LABEL,
                                        IDialogConstants.CANCEL_LABEL };
                        ret = errorHandler.showMessageDialog(Messages.FtpAccount_file_already_exists, null,
                                NLS.bind(Messages.FtpAccount_file_exists_overwrite, filename),
                                MessageDialog.QUESTION, buttons, 0, adaptable);
                    }
                    if (filecount > 1) {
                        switch (ret) {
                        case 0:
                            break;
                        case 1:
                            replaceAll = true;
                            break;
                        case 3:
                            skipAll = true;
                            /* FALL-THROUGH */
                        case 2:
                            if (deleteTransferred)
                                file.delete();
                            continue;
                        default:
                            return -1;
                        }
                    } else {
                        switch (ret) {
                        case 0:
                            break;
                        case 1:
                            if (deleteTransferred)
                                file.delete();
                            continue;
                        default:
                            return -1;
                        }
                    }
                }
                ftp.deleteFile(Core.encodeUrlSegment(filename));
                //               System.out.println(filename + " deleted"); //$NON-NLS-1$
            }
            try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) {
                ftp.storeFile(Core.encodeUrlSegment(filename), in);
                //               System.out.println(filename + " stored"); //$NON-NLS-1$
                n++;
            } finally {
                if (deleteTransferred)
                    file.delete();
                monitor.worked(1);
            }
        }
    }
    return n;
}

From source file:nz.govt.natlib.ndha.common.FileUtils.java

public static void removeFTPDirectory(FTPClient ftpClient, String directoryName) {
    try {//from   ww w .j  a  va  2s. c  om
        ftpClient.changeWorkingDirectory(directoryName);
        for (FTPFile file : ftpClient.listFiles()) {
            if (file.isDirectory()) {
                FileUtils.removeFTPDirectory(ftpClient, file.getName());
            } else {
                log.debug("Deleting " + file.getName());
                ftpClient.deleteFile(file.getName());
            }
        }
        ftpClient.changeWorkingDirectory(directoryName);
        ftpClient.changeToParentDirectory();
        log.debug("Deleting " + directoryName);
        ftpClient.removeDirectory(directoryName);
    } catch (Exception ex) {

    }
}

From source file:org.openconcerto.ftp.FTPUtils.java

static public final void saveR(FTPClient ftp, File local) throws IOException {
    local.mkdirs();//ww w. j  a v a 2 s  .  c  om
    for (FTPFile child : ftp.listFiles()) {
        final String childName = child.getName();
        if (childName.indexOf('.') != 0) {
            if (child.isDirectory()) {
                ftp.changeWorkingDirectory(childName);
                saveR(ftp, new File(local, childName));
                ftp.changeToParentDirectory();
            } else {
                final OutputStream outs = new FileOutputStream(new File(local, childName));
                ftp.retrieveFile(childName, outs);
                outs.close();
            }
        }
    }
}

From source file:org.openconcerto.ftp.FTPUtils.java

static public final void recurse(FTPClient ftp, ExnClosure<FTPFile, ?> c, RecursionType type)
        throws IOException {
    for (FTPFile child : ftp.listFiles()) {
        if (child.getName().indexOf('.') != 0) {
            if (type == RecursionType.BREADTH_FIRST)
                c.executeCheckedWithExn(child, IOException.class);
            if (child.isDirectory()) {
                ftp.changeWorkingDirectory(child.getName());
                recurse(ftp, c, type);/*from  w  w  w .  j av  a2  s.  com*/
                ftp.changeToParentDirectory();
            }
            if (type == RecursionType.DEPTH_FIRST)
                c.executeCheckedWithExn(child, IOException.class);
        }
    }
}

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

public void test05CdUp() {

    FTPClient ftp = setupFTPClient();

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

        assertEmptyDirectory(ftp);/*from   ww  w.  j av  a2 s. c o m*/

        String name1 = "/FTPdir1";

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

        ftp.changeWorkingDirectory(name1);

        String name2 = name1.concat("/").concat("FTPdir2");

        // Create folder by mkdir FTP command
        ftp.makeDirectory(name2);
        ftp.changeWorkingDirectory(name2);

        boolean success = ftp.changeToParentDirectory();
        assertTrue(success);

        String newWorkingDirectory = ftp.printWorkingDirectory();
        assertEquals(name1, newWorkingDirectory);

        ftp.disconnect();

        tx.success();

    } catch (IOException | FrameworkException ex) {
        logger.log(Level.SEVERE, "Error", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
}

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

public void test06CdTwoUp() {

    FTPClient ftp = setupFTPClient();

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

        assertEmptyDirectory(ftp);//from  www.  j av a 2s.  c o  m

        String name1 = "/FTPdir1";

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

        ftp.changeWorkingDirectory(name1);

        String name2 = name1.concat("/").concat("FTPdir2");

        // Create folder by mkdir FTP command
        ftp.makeDirectory(name2);

        ftp.changeWorkingDirectory(name2);

        String name3 = name2.concat("/").concat("FTPdir3");

        // Create folder by mkdir FTP command
        ftp.makeDirectory(name3);

        ftp.changeWorkingDirectory(name3);

        ftp.changeToParentDirectory();
        ftp.changeToParentDirectory();

        String newWorkingDirectory = ftp.printWorkingDirectory();
        assertEquals(name1, newWorkingDirectory);

        ftp.disconnect();

        tx.success();

    } catch (IOException | FrameworkException ex) {
        logger.log(Level.SEVERE, "Error", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
}

From source file:ru.in360.FTPUtil.java

public static void upload(File src, FTPClient ftp) throws IOException {
    if (src.isDirectory()) {
        ftp.makeDirectory(src.getName());
        ftp.changeWorkingDirectory(src.getName());
        for (File file : src.listFiles()) {
            upload(file, ftp);/*from ww w  . j  av a 2  s .  c o m*/
        }
        ftp.changeToParentDirectory();
    } else {
        InputStream srcStream = null;
        try {
            srcStream = src.toURI().toURL().openStream();
            ftp.storeFile(src.getName(), srcStream);
        } finally {
            IOUtils.closeQuietly(srcStream);
        }
    }
}

From source file:websync2.SyncDesign.java

public void copyFileFTP(File temp, File servF) {

    InputStream inputStream = null;
    try {//from  ww  w.  j a  v a2 s .  c  o m
        FTPClient ftpclient = ftp.getFtpClient();
        ftpclient.changeToParentDirectory();
        String firstRemoteFile = servF.getAbsolutePath();
        String[] pathA = firstRemoteFile.split("/");
        if (pathA != null) {
            for (int i = 0; i < pathA.length - 1; i++) {
                if ("".equals(pathA[i])) {
                    continue;
                }
                InputStream is = ftpclient.retrieveFileStream(pathA[i]);
                int retCode = ftpclient.getReplyCode();
                if (retCode == 550) {
                    ftpclient.makeDirectory(pathA[i]);
                }
                ftpclient.changeWorkingDirectory(pathA[i]);
            }
            inputStream = new FileInputStream(temp);
            boolean done = ftpclient.storeFile(pathA[pathA.length - 1], inputStream);
            if (done) {
                System.out.println("The first file is uploaded successfully.");
            }
        }

    } catch (FileNotFoundException ex) {
        Logger.getLogger(SyncDesign.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(SyncDesign.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            inputStream.close();
        } catch (IOException ex) {
            Logger.getLogger(SyncDesign.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}