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

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

Introduction

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

Prototype

public String printWorkingDirectory() throws IOException 

Source Link

Document

Returns the pathname of the current working directory.

Usage

From source file:org.mule.modules.FtpUtils.java

public static FTPFile[] listFiles(FTPClient client, String path) {
    try {/*w w  w .  j  a  v  a  2s .  c  o m*/
        if (path == null || path.isEmpty()) {
            path = client.printWorkingDirectory();
        }
        return client.listFiles(path);
    } catch (IOException e) {
        try {
            client.disconnect();
        } catch (Exception error) {
            throw new FtpLiteException("There was an error disconnecting. " + error.toString());
        }
        throw new FtpLiteException("There was an error fetching files from SFTP");
    }
}

From source file:org.mule.modules.FtpUtils.java

public static InputStream getFileStream(FTPClient client, String filePath, String fileName) {
    try {/*w  w  w  .  j av  a 2 s  . c o  m*/
        if (filePath == null || filePath.isEmpty()) {
            filePath = client.printWorkingDirectory();
        }
        String fullPath = createFullPath(filePath, fileName);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        if (fileExists(client, filePath, fileName)) {
            client.retrieveFile(fullPath, outStream);
            return new ByteArrayInputStream(outStream.toByteArray());
        } else {
            throw new FtpLiteException("File does not exist");
        }
    } catch (IOException e) {
        disconnect(client);
        throw new FtpLiteException("Error retrieving file stream from SFTP");
    }
}

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

static public final void rmR(final FTPClient ftp, final String toRm) throws IOException {
    final String cwd = ftp.printWorkingDirectory();
    // si on ne peut cd, le dossier n'existe pas
    if (ftp.changeWorkingDirectory(toRm)) {
        recurse(ftp, new ExnClosure<FTPFile, IOException>() {
            @Override/*from   ww  w . ja  v  a 2  s  . c o m*/
            public void executeChecked(FTPFile input) throws IOException {
                final boolean res;
                if (input.isDirectory())
                    res = ftp.removeDirectory(input.getName());
                else
                    res = ftp.deleteFile(input.getName());
                if (!res)
                    throw new IOException("unable to delete " + input);
            }
        }, RecursionType.DEPTH_FIRST);
    }
    ftp.changeWorkingDirectory(cwd);
    ftp.removeDirectory(toRm);
}

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

public void test03MkdirCd() {

    FTPClient ftp = setupFTPClient();
    final String name1 = "/FTPdir1";

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

        FTPFile[] dirs = ftp.listDirectories();

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

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

        tx.success();

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

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

        ftp.changeWorkingDirectory(name1);

        assertEmptyDirectory(ftp);

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

        ftp.disconnect();

        tx.success();

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

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

public void test04MkdirCdMkdirCd() {

    FTPClient ftp = setupFTPClient();

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

        assertEmptyDirectory(ftp);/*from w ww  . j  a  v a  2  s.com*/

        String name1 = "/FTPdir1";

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

        ftp.changeWorkingDirectory(name1);

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

        assertEmptyDirectory(ftp);

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

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

        ftp.changeWorkingDirectory(name2);

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

        assertEmptyDirectory(ftp);

        ftp.disconnect();

        tx.success();

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

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 a  v a 2  s . co 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   ww  w .  j  a v 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: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  .  ja  v a 2s . 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  va 2 s.  c  o  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.ftp.FtpDirectoriesTest.java

public void test03MkdirCd() {

    FTPClient ftp = setupFTPClient();
    final String name1 = "/FTPdir1";

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

        FTPFile[] dirs = ftp.listDirectories();

        assertNotNull(dirs);//from  w  w w.  j a va 2s.  c o  m
        assertEquals(0, dirs.length);

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

        tx.success();

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

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

        ftp.changeWorkingDirectory(name1);

        assertEmptyDirectory(ftp);

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

        ftp.disconnect();

        tx.success();

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