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

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

Introduction

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

Prototype

public FTPFile[] listFiles() throws IOException 

Source Link

Document

Using the default system autodetect mechanism, obtain a list of file information for the current working directory.

Usage

From source file:org.shept.util.FtpFileCopy.java

/**
 * List the file directory as specified by the name filter and the path directory
 * The maps keys contain name and modification date for comparison
 * @throws IOException //from   w  w w  .j av a2  s .  c o m
 */
public SortedMap<FileNameDate, FTPFile> fileMapByNameAndDate(FTPClient ftp, String filePattern)
        throws IOException {
    SortedMap<FileNameDate, FTPFile> newFileMap = new TreeMap<FileNameDate, FTPFile>();
    FTPFile[] files = ftp.listFiles();
    for (FTPFile ftpFile : files) {
        if (acceptFile(ftpFile, filePattern)) {
            ftpFile.getTimestamp();
            FileNameDate fk = new FileNameDate(ftpFile.getName(), ftpFile.getTimestamp().getTimeInMillis());
            newFileMap.put(fk, ftpFile);
        }
    }
    return newFileMap;
}

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  . jav  a 2 s.co  m*/
        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);//  w w  w .j a  v 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);/* w  w  w. ja  va  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());
    }

    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 ww .  j  a  va 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 www  . 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());
    }

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

    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  a  va  2 s  .  co m*/
        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) {
        logger.log(Level.WARNING, "", ex);
        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) {
        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(name1, fileNames[0]);
        assertEquals(name2, fileNames[1]);

        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 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  a v a 2s .co m*/
        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());
    }

    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) {
        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(name1, fileNames[0]);
        assertEquals(name2, fileNames[1]);

        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 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  .j  a v a 2  s  .c om
        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 ww w .  j  av  a  2s.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());
    }
}