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

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

Introduction

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

Prototype

public boolean rename(String from, String to) throws IOException 

Source Link

Document

Renames a remote file.

Usage

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

public void test02RenameFile() {

    FTPClient ftp = setupFTPClient();
    final String name2 = "file2";
    final String name1 = "file1";

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

        FTPFile[] files = ftp.listFiles();

        assertNotNull(files);/*from   www.  ja  v a 2  s . co  m*/
        assertEquals(0, files.length);

        // Create files by API methods
        createFTPFile(null, name1);

        tx.success();

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

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

        ftp.rename(name1, name2);

        tx.success();

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

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

        String[] fileNames = ftp.listNames();

        assertNotNull(fileNames);
        assertEquals(1, fileNames.length);
        assertEquals(name2, fileNames[0]);

        ftp.disconnect();

        tx.success();

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

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

public void test03MoveFile() {

    FTPClient ftp = setupFTPClient();
    final String name1 = "file1";
    final String name2 = "dir1";

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

        FTPFile[] files = ftp.listFiles();

        assertNotNull(files);//from  ww w.j  av  a  2 s .c o  m
        assertEquals(0, files.length);

        // Create files by API methods
        createFTPFile(null, name1);

        tx.success();

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

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

        // Create folder in /
        createFTPDirectory(null, name2);
        tx.success();

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

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

        // Move file to dir
        ftp.rename("/" + name1, "/" + name2 + "/" + name1);
        tx.success();

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

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

        ftp.changeWorkingDirectory("/" + name2);

        String[] fileNames = ftp.listNames();

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

        ftp.disconnect();

        tx.success();

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

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

public void test04MoveFileToRoot() {

    FTPClient ftp = setupFTPClient();

    final String name1 = "file1";
    final String name2 = "dir1";

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

        FTPFile[] files = ftp.listFiles();

        assertNotNull(files);//from  w ww . j ava2 s .c  om
        assertEquals(0, files.length);

        // Create files by API methods
        createFTPFile(null, name1);
        tx.success();

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

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

        // Create folder in /
        createFTPDirectory(null, name2);
        tx.success();

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

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

        // Move file to dir
        ftp.rename("/" + name1, "/" + name2 + "/" + name1);
        tx.success();

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

    String[] fileNames = null;
    try (final Tx tx = app.tx()) {

        ftp.changeWorkingDirectory("/" + name2);

        fileNames = ftp.listNames();

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

        // Move file back to /
        ftp.rename("/" + name2 + "/" + name1, "/" + name1);
        tx.success();

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

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

        ftp.changeWorkingDirectory("/");
        tx.success();

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

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

        fileNames = ftp.listNames();

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

        ftp.disconnect();

        tx.success();

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

From source file:org.structr.ftp.FtpFilesTest.java

public void test02RenameFile() {

    FTPClient ftp = setupFTPClient();
    final String name2 = "file2";
    final String name1 = "file1";

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

        FTPFile[] files = ftp.listFiles();

        assertNotNull(files);//from   w ww  .java2 s .  com
        assertEquals(0, files.length);

        // Create files by API methods
        createFTPFile(null, name1);

        tx.success();

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

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

        ftp.rename(name1, name2);

        tx.success();

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

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

        String[] fileNames = ftp.listNames();

        assertNotNull(fileNames);
        assertEquals(1, fileNames.length);
        assertEquals(name2, fileNames[0]);

        ftp.disconnect();

        tx.success();

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

From source file:org.structr.ftp.FtpFilesTest.java

public void test03MoveFile() {

    FTPClient ftp = setupFTPClient();
    final String name1 = "file1";
    final String name2 = "dir1";

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

        FTPFile[] files = ftp.listFiles();

        assertNotNull(files);/*from  w w  w  .  j  av  a 2  s.co m*/
        assertEquals(0, files.length);

        // Create files by API methods
        createFTPFile(null, name1);

        tx.success();

    } catch (Exception ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

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

        // Create folder in /
        createFTPDirectory(null, name2);
        tx.success();

    } catch (Exception ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

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

        // Move file to dir
        ftp.rename("/" + name1, "/" + name2 + "/" + name1);
        tx.success();

    } catch (Exception ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

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

        ftp.changeWorkingDirectory("/" + name2);

        String[] fileNames = ftp.listNames();

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

        ftp.disconnect();

        tx.success();

    } catch (Exception ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
}

From source file:org.structr.ftp.FtpFilesTest.java

public void test04MoveFileToRoot() {

    FTPClient ftp = setupFTPClient();

    final String name1 = "file1";
    final String name2 = "dir1";

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

        FTPFile[] files = ftp.listFiles();

        assertNotNull(files);/*from  w w w .ja v  a  2 s. co  m*/
        assertEquals(0, files.length);

        // Create files by API methods
        createFTPFile(null, name1);
        tx.success();

    } catch (Exception ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

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

        // Create folder in /
        createFTPDirectory(null, name2);
        tx.success();

    } catch (Exception ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

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

        // Move file to dir
        ftp.rename("/" + name1, "/" + name2 + "/" + name1);
        tx.success();

    } catch (Exception ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

    String[] fileNames = null;
    try (final Tx tx = app.tx()) {

        ftp.changeWorkingDirectory("/" + name2);

        fileNames = ftp.listNames();

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

        // Move file back to /
        ftp.rename("/" + name2 + "/" + name1, "/" + name1);
        tx.success();

    } catch (Exception ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

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

        ftp.changeWorkingDirectory("/");
        tx.success();

    } catch (Exception ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

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

        fileNames = ftp.listNames();

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

        ftp.disconnect();

        tx.success();

    } catch (Exception ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
}

From source file:tufts.oki.remoteFiling.RemoteCabinet.java

/**
 *  Rename the file corresponding to this cabinet and update it's display
 *  name to the new name.// ww  w  .j a  v  a2 s. c  om
 *
 *  @author Mark Norton
 */
public void rename(String newName) throws osid.filing.FilingException {
    //  Check the name of the directory on the remote file system.
    try {
        FTPClient client = rc.getClient();
        if (getParent() == null)
            client.rename(getFullName(), rc.getRootBase() + "/" + newName);
        else
            client.rename(getFullName(), ((RemoteCabinet) getParent()).getFullName() + "/" + newName);
    } catch (java.io.IOException ex) {
        throw new osid.filing.FilingException(osid.filing.FilingException.IO_ERROR);
    }

    //  Change the name of the Cabinet.
    updateDisplayName(newName);
}