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

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

Introduction

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

Prototype

public FTPFile[] listDirectories() throws IOException 

Source Link

Document

Using the default system autodetect mechanism, obtain a list of directories contained in the current working directory.

Usage

From source file:de.idos.updates.lookup.FtpLookup.java

private Version findLatestVersion() throws IOException {
    FTPClient ftpClient = new FTPClient();
    ftpClient.connect(inetAddress);/*from  ww  w.  j ava  2  s  .  c  o  m*/
    ftpClient.enterLocalPassiveMode();

    if (login != null) {
        ftpClient.login(login, null);
    }

    if (workingDir != null) {
        ftpClient.changeWorkingDirectory(workingDir);
    }

    FTPFile[] availableVersionsDirs = ftpClient.listDirectories();
    List<String> strings = new ArrayList<String>();
    for (FTPFile ftpFile : availableVersionsDirs) {
        strings.add(ftpFile.getName());
    }

    List<Version> versions = new VersionFactory().createVersionsFromStrings(strings);
    return new VersionFinder().findLatestVersion(versions);
}

From source file:adams.flow.source.FTPLister.java

/**
 * Executes the flow item./*  w w w  .j av  a  2 s .com*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    FTPClient client;
    FTPFile[] files;

    result = null;

    m_Queue.clear();
    client = m_Connection.getFTPClient();
    if (m_ListDirs) {
        try {
            if (m_RemoteDir.length() > 0)
                client.changeWorkingDirectory(m_RemoteDir);
            files = client.listDirectories();
            for (FTPFile file : files) {
                if (isStopped())
                    break;
                if (file == null)
                    continue;
                if (m_RegExp.isEmpty() || m_RegExp.isMatchAll() || m_RegExp.isMatch(file.getName()))
                    m_Queue.add(file.getName());
            }
        } catch (Exception e) {
            result = handleException("Failed to list directories in '" + m_RemoteDir + "': ", e);
        }
    }

    if (result == null) {
        if (m_ListFiles) {
            try {
                if (m_RemoteDir.length() > 0)
                    client.changeWorkingDirectory(m_RemoteDir);
                files = client.listFiles();
                for (FTPFile file : files) {
                    if (isStopped())
                        break;
                    if (file == null)
                        continue;
                    if (file.isDirectory())
                        continue;
                    if (m_RegExp.isEmpty() || m_RegExp.isMatchAll() || m_RegExp.isMatch(file.getName()))
                        m_Queue.add(file.getName());
                }
            } catch (Exception e) {
                result = handleException("Failed to list files in '" + m_RemoteDir + "': ", e);
            }
        }
    }

    if (isStopped())
        m_Queue.clear();

    return result;
}

From source file:com.thebigbang.ftpclient.FTPOperation.java

/**
 * will force keep the device turned on for all the operation duration.
 * @param params//from w  ww.  j  a v  a  2  s . c  om
 * @return 
 */
@SuppressLint("Wakelock")
@SuppressWarnings("deprecation")
@Override
protected Boolean doInBackground(FTPBundle... params) {
    Thread.currentThread().setName("FTPOperationWorker");
    for (final FTPBundle bundle : params) {

        FTPClient ftp = new FTPClient();
        PowerManager pw = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
        WakeLock w = pw.newWakeLock(PowerManager.FULL_WAKE_LOCK, "FTP Client");
        try {
            // setup ftp connection:
            InetAddress addr = InetAddress.getByName(bundle.FTPServerHost);
            //set here the timeout.
            TimeoutThread timeout = new TimeoutThread(_timeOut, new FTPTimeout() {
                @Override
                public void Occurred(TimeoutException e) {
                    bundle.Exception = e;
                    bundle.OperationStatus = FTPOperationStatus.Failed;
                    publishProgress(bundle);
                }
            });
            timeout.start();
            ftp.connect(addr, bundle.FTPServerPort);
            int reply = ftp.getReplyCode();
            timeout.Stop();
            if (!FTPReply.isPositiveCompletion(reply)) {
                throw new IOException("connection refuse");
            }
            ftp.login(bundle.FTPCredentialUsername, bundle.FTPCredentialPassword);
            if (bundle.OperationType == FTPOperationType.Connect) {
                bundle.OperationStatus = FTPOperationStatus.Succed;
                publishProgress(bundle);
                continue;
            }
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();

            w.acquire();
            // then switch between enum of operation types.
            if (bundle.OperationType == FTPOperationType.RetrieveFilesFoldersList) {
                ftp.changeWorkingDirectory(bundle.RemoteWorkingDirectory);
                bundle.FilesOnCurrentPath = ftp.listFiles();
                bundle.FoldersOnCurrentPath = ftp.listDirectories();
                bundle.OperationStatus = FTPOperationStatus.Succed;
            } else if (bundle.OperationType == FTPOperationType.RetrieveFolderList) {
                ftp.changeWorkingDirectory(bundle.RemoteWorkingDirectory);
                bundle.FoldersOnCurrentPath = ftp.listDirectories();
                bundle.OperationStatus = FTPOperationStatus.Succed;
            } else if (bundle.OperationType == FTPOperationType.RetrieveFileList) {
                ftp.changeWorkingDirectory(bundle.RemoteWorkingDirectory);
                bundle.FilesOnCurrentPath = ftp.listFiles();
                bundle.OperationStatus = FTPOperationStatus.Succed;
            } else if (bundle.OperationType == FTPOperationType.GetData) {
                String finalFPFi = bundle.LocalFilePathName;
                // The remote filename to be downloaded.
                if (bundle.LocalWorkingDirectory != null && bundle.LocalWorkingDirectory != "") {
                    File f = new File(bundle.LocalWorkingDirectory);
                    f.mkdirs();

                    finalFPFi = bundle.LocalWorkingDirectory + finalFPFi;
                }
                FileOutputStream fos = new FileOutputStream(finalFPFi);

                // Download file from FTP server
                String finalFileN = bundle.RemoteFilePathName;
                if (bundle.RemoteWorkingDirectory != null && bundle.RemoteWorkingDirectory != "") {
                    finalFileN = bundle.RemoteWorkingDirectory + finalFileN;
                }
                boolean b = ftp.retrieveFile(finalFileN, fos);
                if (b)
                    bundle.OperationStatus = FTPOperationStatus.Succed;
                else
                    bundle.OperationStatus = FTPOperationStatus.Failed;
                fos.close();

            } else if (bundle.OperationType == FTPOperationType.SendData) {
                InputStream istr = new FileInputStream(bundle.LocalFilePathName);
                ftp.changeWorkingDirectory(bundle.RemoteWorkingDirectory);
                Boolean b = ftp.storeFile(bundle.RemoteFilePathName, istr);
                istr.close();
                if (b)
                    bundle.OperationStatus = FTPOperationStatus.Succed;
                else
                    bundle.OperationStatus = FTPOperationStatus.Failed;
            } else if (bundle.OperationType == FTPOperationType.DeleteData) {
                throw new IOException("DeleteData is Not yet implemented");
            }

            ftp.disconnect();
            // then finish/return.
            //publishProgress(bundle);
        } catch (IOException e) {
            e.printStackTrace();
            bundle.Exception = e;
            bundle.OperationStatus = FTPOperationStatus.Failed;
        }
        try {
            w.release();
        } catch (RuntimeException ex) {
            ex.printStackTrace();
        }
        publishProgress(bundle);
    }
    return true;
}

From source file:org.alinous.ftp.FtpManager.java

public FTPFile[] listDirectories(String sessionId) throws IOException {
    FTPClient ftp = this.instances.get(sessionId).getFtp();

    FTPFile[] files = ftp.listDirectories();

    return files;
}

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

public void test01ListDirectories() {

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

    FTPClient ftp = setupFTPClient();
    FTPFile[] dirs = null;//from  w w w .ja va 2 s . 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) {
        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 /
        createFTPDirectory(null, name2);

        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());
    }
}

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

public void test02MkDir() {

    FTPClient ftp = setupFTPClient();

    FTPFile[] dirs = null;//from  w w w. ja va 2  s  .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());
    }
}

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);/*  w w w. j  ava2  s .  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 test07CdToSiblingDirectory() {

    FTPClient ftp = setupFTPClient();

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

        FTPFile[] dirs = ftp.listDirectories();

        assertNotNull(dirs);// w  w  w .  j  a 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);/* 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 test01ListDirectories() {

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

    FTPClient ftp = setupFTPClient();
    FTPFile[] dirs = null;/* w  w w  .  j  a va2 s .  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());
    }
}