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

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

Introduction

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

Prototype

@Override
public void disconnect() throws IOException 

Source Link

Document

Closes the connection to the FTP server and restores connection parameters to the default values.

Usage

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 w w  w . j  a  v a 2 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);

        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:org.structr.files.ftp.FtpDirectoriesTest.java

public void test07CdToSiblingDirectory() {

    FTPClient ftp = setupFTPClient();

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

        FTPFile[] dirs = ftp.listDirectories();

        assertNotNull(dirs);/* w w  w . j av  a 2 s  . c  om*/
        assertEquals(0, dirs.length);

        String name1 = "/FTPdir1";
        String name2 = "/FTPdir2";

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

        ftp.changeWorkingDirectory(name1);

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

        ftp.changeWorkingDirectory("../" + name2);

        newWorkingDirectory = ftp.printWorkingDirectory();
        assertEquals(name2, newWorkingDirectory);

        ftp.disconnect();

        tx.success();

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

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

public void test08CdRoot() {

    FTPClient ftp = setupFTPClient();

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

        FTPFile[] dirs = ftp.listDirectories();

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

        String name1 = "/FTPdir1";

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

        ftp.changeWorkingDirectory(name1);

        assertEmptyDirectory(ftp);

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

        ftp.changeWorkingDirectory("/");

        newWorkingDirectory = ftp.printWorkingDirectory();
        assertEquals("/", newWorkingDirectory);

        ftp.disconnect();

        tx.success();

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

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

public void test00StoreFile() {

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

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

        FTPFile[] files = ftp.listFiles();

        assertNotNull(files);//  www . j  av a 2 s . com
        assertEquals(0, files.length);

        ftp.setFileType(FTP.ASCII_FILE_TYPE);
        ftp.setAutodetectUTF8(true);

        // Store a file
        InputStream in = IOUtils.toInputStream("Test Content");
        ftp.storeFile(name1, in);

        in.close();

        tx.success();

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

    String[] fileNames = null;

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

        fileNames = ftp.listNames();

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

        // Create second file in /
        createFTPFile(null, name2);

        tx.success();

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

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

        fileNames = ftp.listNames();

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

        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 test01ListFiles() {

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

    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 (IOException | FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex.getMessage());
    }

    String[] fileNames = null;

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

        fileNames = ftp.listNames();

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

        // Create second file in /
        createFTPFile(null, name2);

        tx.success();

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

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

        fileNames = ftp.listNames();

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

        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 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 ww w . j  av  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  w  w w  .j  a  v a2 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);//  w  ww  .  j a  v a2 s .com
        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.FtpDirectoriesTest.java

public void test01ListDirectories() {

    final String name1 = "FTPdir1";
    final String name2 = "FTPdir2";

    FTPClient ftp = setupFTPClient();
    FTPFile[] dirs = null;/*from ww w . j  av  a  2s .  c  om*/

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

        dirs = ftp.listDirectories();

        assertNotNull(dirs);
        assertEquals(0, dirs.length);

        // Create folder by API methods
        createFTPDirectory(null, name1);

        tx.success();

    } catch (IOException | FrameworkException ex) {
        logger.log(Level.WARNING, "", ex);
        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 /
        createFTPDirectory(null, name2);

        tx.success();

    } catch (IOException | FrameworkException ex) {
        logger.log(Level.WARNING, "", ex);
        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) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
}

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

public void test02MkDir() {

    FTPClient ftp = setupFTPClient();

    FTPFile[] dirs = null;//ww w . jav  a  2  s. co  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) {
        logger.log(Level.WARNING, "", ex);
        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) {
        logger.log(Level.WARNING, "", ex);
        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) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
}