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:Main.java

public static void main(String[] args) {
    FTPClient client = new FTPClient();

    client.connect("ftp.domain.com");
    client.login("admin", "secret");

    String[] names = client.listNames();
    for (String name : names) {
        System.out.println("Name = " + name);
    }/*  www  .  j a v a  2  s  . c  om*/

    FTPFile[] ftpFiles = client.listFiles();
    for (FTPFile ftpFile : ftpFiles) {
        // Check if FTPFile is a regular file
        if (ftpFile.getType() == FTPFile.FILE_TYPE) {
            System.out.println("FTPFile: " + ftpFile.getName() + "; "
                    + FileUtils.byteCountToDisplaySize(ftpFile.getSize()));
        }
    }
    client.logout();
    client.disconnect();
}

From source file:com.feilong.tools.net.ZClientTest.java

/**
 * Gets the files./*from w w w .  j a  v  a2  s  .c o m*/
 * 
 * @param ftp
 *            the ftp
 * @param localDir
 *            the local dir
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
private static void testGetFiles(FTPClient ftp, File localDir) throws IOException {
    String[] names = ftp.listNames();
    for (String name : names) {
        File file = new File(localDir.getPath() + File.separator + name);
        if (!file.exists()) {
            file.createNewFile();
        }
        long pos = file.length();
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.seek(pos);
        ftp.setRestartOffset(pos);
        InputStream is = ftp.retrieveFileStream(name);
        if (is == null) {
            log.info("no such file:" + name);
        } else {
            log.info("start getting file:" + name);
            int b;
            while ((b = is.read()) != -1) {
                raf.write(b);
            }
            is.close();
            if (ftp.completePendingCommand()) {
                log.info("done!");
            } else {
                log.info("can't get file:" + name);
            }
        }
        raf.close();
    }
}

From source file:controller.FTPServerController.java

public static List<String> findFilesXMLFTPServer(FTPClient ftpClient)
        throws UnsupportedEncodingException, IOException {

    List<String> listFileXML = new ArrayList<>();
    Map<String, String> mapXMLFiles = new HashMap<>();

    try {/*from w ww .j  a v  a2s.c  o  m*/
        String[] allFiles = ftpClient.listNames();

        if (allFiles.length > 0) {
            System.out.println("Listing files '.xml':");

            for (String itemAllFilles : allFiles) {
                if (itemAllFilles.endsWith(".xml")) {
                    listFileXML.add(itemAllFilles);
                    System.out.println("    " + itemAllFilles);
                }
            }
        } else {
            System.out.println("No exists File .xml in this FTP Server!");
        }
    } catch (NullPointerException | SocketException e) {
        System.out.println(e.getMessage());
    }

    return listFileXML;
}

From source file:ftp.FTPtask.java

/**
 * ? ? ? ??      Folder/*from  w w w. ja  v  a  2s .  c  o m*/
 * @param FTPADDR, ?  ?
 * @param user,   
 * @param password,   
 * @param Folder,    ?? ?
 * @return String[] result, ?? /
 */

public static String[] GetList(String FTPADDR, String user, String password, String Folder) {
    String[] result;
    result = null;

    FTPClient client = new FTPClient();

    try {
        client.connect(FTPADDR);
        client.login(user, password);
        client.changeWorkingDirectory(Folder);
        result = client.listNames();
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:CFDI_Verification.CFDI_Verification.java

public static void list_directories(String directory) {
    String server = "192.1.1.64";
    String user = "ftpuser";
    String pass = "Oracle123";
    String result = "No Connected!";

    FTPClient ftpClient = new FTPClient();

    try {/* w ww  . j a  v a 2  s  .c o  m*/
        ftpClient.connect(server);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.changeWorkingDirectory(directory);

        String[] directories = ftpClient.listNames();
        String dir;

        for (int i = 0; i < directories.length; i++) {
            System.out.println(directories[i]);
            dir = directories[i];
        }

        ftpClient.logout();
        ftpClient.disconnect();

    } catch (IOException ex) {
        System.out.println("Ooops! Error en conexin." + ex.getMessage());
    }
}

From source file:CFDI_Verification.CFDI_Verification.java

public static ARRAY getDirectories(String directory) throws SQLException {
    Connection conn = new OracleDriver().defaultConnection();
    ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("DIRECTORIES_LIST", conn);

    String server = "192.1.1.64";
    String user = "ftpuser";
    String pass = "Oracle123";
    String result = "No Connected!";

    FTPClient ftpClient = new FTPClient();

    try {/*  w w  w  .ja va  2  s  . com*/
        ftpClient.connect(server);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.changeWorkingDirectory(directory);

        String[] directories = ftpClient.listNames();
        ARRAY directories_list = new ARRAY(descriptor, conn, directories);

        ftpClient.logout();
        ftpClient.disconnect();

        return directories_list;
    } catch (IOException ex) {
        System.out.println("Ooops! Error en conexin." + ex.getMessage());
    }
    return null;
}

From source file:deincraftlauncher.IO.download.FTPSync.java

public static String[] listFiles(String dir) {

    FTPClient client = new FTPClient();

    try {/*from w  w w . j a  va 2s. c  om*/
        client.connect(ftpServer);
        client.login(ftpUsername, ftpPassword);
        client.changeWorkingDirectory(dir);

        // Obtain a list of filenames in the current working
        // directory. When no file found an empty array will 
        // be returned.
        String[] names = client.listNames();

        client.logout();

        return names;

    } catch (IOException e) {
        System.err.println("Error connecting to ftp (listfiles): " + e);
    } finally {

        try {
            client.disconnect();
        } catch (IOException e) {
            System.err.println("Error disconnecting to ftp (listfiles): " + e);
        }
    }

    return null;

}

From source file:org.compiere.model.MMediaServer.java

/**
 *    (Re-)Deploy all media/* w ww.  ja  va  2  s.com*/
 *    @param media array of media to deploy
 *    @return true if deployed
 */
public boolean deploy(MMedia[] media) {
    // Check whether the host is our example localhost, we will not deploy locally, but this is no error
    if (this.getIP_Address().equals("127.0.0.1") || this.getName().equals("localhost")) {
        log.warning("You have not defined your own server, we will not really deploy to localhost!");
        return true;
    }

    FTPClient ftp = new FTPClient();
    try {
        ftp.connect(getIP_Address());
        if (ftp.login(getUserName(), getPassword()))
            log.info("Connected to " + getIP_Address() + " as " + getUserName());
        else {
            log.warning("Could NOT connect to " + getIP_Address() + " as " + getUserName());
            return false;
        }
    } catch (Exception e) {
        log.log(Level.WARNING, "Could NOT connect to " + getIP_Address() + " as " + getUserName(), e);
        return false;
    }

    boolean success = true;
    String cmd = null;
    //   List the files in the directory
    try {
        cmd = "cwd";
        ftp.changeWorkingDirectory(getFolder());
        //
        cmd = "list";
        String[] fileNames = ftp.listNames();
        log.log(Level.FINE, "Number of files in " + getFolder() + ": " + fileNames.length);

        /*
        FTPFile[] files = ftp.listFiles();
        log.config("Number of files in " + getFolder() + ": " + files.length);
        for (int i = 0; i < files.length; i++)
           log.fine(files[i].getTimestamp() + " \t" + files[i].getName());*/
        //
        cmd = "bin";
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        //
        for (int i = 0; i < media.length; i++) {
            if (!media[i].isSummary()) {
                log.log(Level.INFO, " Deploying Media Item:" + media[i].get_ID() + media[i].getExtension());
                MImage thisImage = media[i].getImage();

                // Open the file and output streams
                byte[] buffer = thisImage.getData();
                ByteArrayInputStream is = new ByteArrayInputStream(buffer);

                String fileName = media[i].get_ID() + media[i].getExtension();
                cmd = "put " + fileName;
                ftp.storeFile(fileName, is);
                is.close();
            }
        }
    } catch (Exception e) {
        log.log(Level.WARNING, cmd, e);
        success = false;
    }
    //   Logout from the FTP Server and disconnect
    try {
        cmd = "logout";
        ftp.logout();
        cmd = "disconnect";
        ftp.disconnect();
    } catch (Exception e) {
        log.log(Level.WARNING, cmd, e);
    }
    ftp = null;
    return success;
}

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 .  ja  v a 2  s .  c  om
        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 .ja  v  a 2s.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());
    }
}