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

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

Introduction

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

Prototype

public String[] listNames() throws IOException 

Source Link

Document

Obtain a list of filenames in the current working directory This information is obtained through the NLST command.

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

    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);/*w  ww  .ja v  a  2  s  .co  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 w  w .j  a  v a 2  s  . co  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  w w w .  j  a  v  a 2s  .  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);//  w  ww. j a  v  a2  s  .c  o 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  ww w .  j  ava2  s.  c o 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());
    }

    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  ww . ja v a  2s. c  o 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 ww  .  j  ava2  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:srv.update.java

public void getVersao(View view) {
    String server = "ftp.atmatech.com.br";
    int port = 21;
    String user = "atmatech";
    String pass = "ftpp2015";
    FTPClient ftpClient = new FTPClient();
    try {//from w  w  w. jav  a2  s .com

        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.changeWorkingDirectory("/atmatech.com.br/atualizacaosac/");

        // APPROACH #1: using retrieveFile(String, OutputStream)
        String[] arq = ftpClient.listNames();
        for (String f : arq) {
            if (!f.equals(".")) {
                if (!f.equals("..")) {
                    if (f.contains(".")) {
                        view.jLprogresso.setText(f);
                        String remoteFile1 = "/atmatech.com.br/atualizacaosac/" + f;
                        String destino = ".\\" + f;
                        File downloadFile1 = new File(destino);
                        OutputStream outputStream1 = new BufferedOutputStream(
                                new FileOutputStream(downloadFile1));
                        boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
                        outputStream1.close();
                        if (!success) {
                            JOptionPane.showMessageDialog(null, "Erro Em Baixar Verso");
                            System.exit(0);
                        }
                    } else {
                        ftpClient.changeWorkingDirectory("/atmatech.com.br/atualizacaosac/" + f);
                        String[] arq2 = ftpClient.listNames();
                        for (String f2 : arq2) {
                            if (!f2.equals(".")) {
                                if (!f2.equals("..")) {
                                    view.jLprogresso.setText(f2);
                                    File diretorio = new File(".\\" + f);
                                    if (!diretorio.exists()) {
                                        diretorio.mkdirs(); //mkdir() cria somente um diretrio, mkdirs() cria diretrios e subdiretrios.
                                    }
                                    String remoteFile1 = "/atmatech.com.br/atualizacaosac/" + f + "/" + f2;
                                    String destino = ".\\" + f + "\\" + f2;
                                    File downloadFile1 = new File(destino);
                                    OutputStream outputStream1 = new BufferedOutputStream(
                                            new FileOutputStream(downloadFile1));
                                    boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
                                    outputStream1.close();
                                    if (!success) {
                                        JOptionPane.showMessageDialog(null, "Erro Em Baixar Verso");
                                        System.exit(0);
                                    }
                                }
                            }
                        }

                    }

                }
            }
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "Erro \n" + ex);
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Erro \n" + ex);
        }
    }
}

From source file:TrabajoFinalJava.CrearListaFicheros.java

@Override
public void run() {

    String ftpSsrver = "127.0.0.1";
    String ftpUser = "solera";
    String ftpPass = "solera";

    FTPClient cFtp = new FTPClient();

    try {/*from  w  w  w . jav a2  s  . c om*/

        cFtp.connect(ftpSsrver);
        boolean login = cFtp.login(ftpUser, ftpPass);
        System.out.print("conexion ftp para ver ficheros establecida");

        cFtp.enterLocalPassiveMode();

        String[] archivos = cFtp.listNames();
        FTPFile[] detalles = cFtp.listFiles();

        archivos = cFtp.listNames();

        for (int i = 0; i < archivos.length; i++) {

            arrayArchivos.add(archivos[i]);
            arrayArchivosDetalles.add(archivos[i]);

        }

        cFtp.logout();
        cFtp.disconnect();
        System.out.println("Conexion Finalizada, buenas tardes.");
    } catch (IOException ioe) {
        System.out.println("error" + ioe.toString());
    }

}