Example usage for org.apache.commons.net.ftp FTPListParseEngine getFiles

List of usage examples for org.apache.commons.net.ftp FTPListParseEngine getFiles

Introduction

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

Prototype

public FTPFile[] getFiles() throws IOException 
    

Source Link

Document

Returns an array of FTPFile objects containing the whole list of files returned by the server as read by this object's parser.

Usage

From source file:madkitgroupextension.export.Export.java

public static void updateFTP(FTPClient ftpClient, String _directory_dst, File _directory_src,
        File _current_file_transfert) throws IOException, TransfertException {
    ftpClient.changeWorkingDirectory("./");
    FTPListParseEngine ftplpe = ftpClient.initiateListParsing(_directory_dst);
    FTPFile files[] = ftplpe.getFiles();

    File current_file_transfert = _current_file_transfert;

    try {/*from w w w .jav  a  2s  . c  o m*/
        for (File f : _directory_src.listFiles()) {
            if (f.isDirectory()) {
                if (!f.getName().equals("./") && !f.getName().equals("../")) {
                    if (_current_file_transfert != null) {
                        if (!_current_file_transfert.getCanonicalPath().startsWith(f.getCanonicalPath()))
                            continue;
                        else
                            _current_file_transfert = null;
                    }
                    boolean found = false;
                    for (FTPFile ff : files) {
                        if (f.getName().equals(ff.getName())) {
                            if (ff.isFile()) {
                                ftpClient.deleteFile(_directory_dst + ff.getName());
                            } else
                                found = true;
                            break;
                        }
                    }

                    if (!found) {
                        ftpClient.changeWorkingDirectory("./");
                        if (!ftpClient.makeDirectory(_directory_dst + f.getName() + "/"))
                            System.err.println(
                                    "Impossible to create directory " + _directory_dst + f.getName() + "/");
                    }
                    updateFTP(ftpClient, _directory_dst + f.getName() + "/", f, _current_file_transfert);
                }
            } else {
                if (_current_file_transfert != null) {
                    if (!_current_file_transfert.equals(f.getCanonicalPath()))
                        continue;
                    else
                        _current_file_transfert = null;
                }
                current_file_transfert = _current_file_transfert;
                FTPFile found = null;
                for (FTPFile ff : files) {
                    if (f.getName().equals(ff.getName())) {
                        if (ff.isDirectory()) {
                            FileTools.removeDirectory(ftpClient, _directory_dst + ff.getName());
                        } else
                            found = ff;
                        break;
                    }
                }
                if (found == null || (found.getTimestamp().getTimeInMillis() - f.lastModified()) < 0
                        || found.getSize() != f.length()) {
                    FileInputStream fis = new FileInputStream(f);
                    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                    if (!ftpClient.storeFile(_directory_dst + f.getName(), fis))
                        System.err.println("Impossible to send file: " + _directory_dst + f.getName());
                    fis.close();
                    for (FTPFile ff : ftplpe.getFiles()) {
                        if (f.getName().equals(ff.getName())) {
                            f.setLastModified(ff.getTimestamp().getTimeInMillis());
                            break;
                        }
                    }
                }
            }

        }
    } catch (IOException e) {
        throw new TransfertException(current_file_transfert, null, e);
    }
    for (FTPFile ff : files) {
        if (!ff.getName().equals(".") && !ff.getName().equals("..")) {
            boolean found = false;
            for (File f : _directory_src.listFiles()) {
                if (f.getName().equals(ff.getName()) && f.isDirectory() == ff.isDirectory()) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                if (ff.isDirectory()) {
                    FileTools.removeDirectory(ftpClient, _directory_dst + ff.getName());
                } else {
                    ftpClient.deleteFile(_directory_dst + ff.getName());
                }
            }
        }
    }
}

From source file:net.seedboxer.common.ftp.FtpUploaderCommons.java

/**
 * List files inside the current folder.
 * /*  ww w. jav a 2 s  .co m*/
 * @return List with files names and size
 * @throws IOException
 */
private Map<String, Long> listFiles() throws FtpException {
    int attempts = 0;
    Map<String, Long> files = new LinkedHashMap<String, Long>();
    while (true) {
        try {
            FTPListParseEngine engine = null;
            if (type.startsWith("UNIX")) {
                engine = ftpClient.initiateListParsing(FTPClientConfig.SYST_UNIX, null);
            } else {
                engine = ftpClient.initiateListParsing();
            }

            FTPFile[] list = engine.getFiles();
            if (list != null) {
                for (FTPFile ftpFile : list) {
                    files.put(ftpFile.getName(), ftpFile.getSize());
                }
            }
            return files;
        } catch (Exception e) {
            attempts++;
            if (attempts > 3) {
                throw new FtpListFilesException(e);
            } else {
                LOGGER.trace("First attempt to get list of files FAILED! attempt={}", attempts);
            }
        }
    }
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

/**
 * Generate a directory listing using the MLSD command.
 *
 * @param pathname the directory name, may be {@code null}
 * @return the array of file entries//from   w  w  w . j av a2 s. co  m
 * @throws IOException
 * @since 3.0
 */
public FTPFile[] mlistDir(String pathname) throws IOException {
    FTPListParseEngine engine = initiateMListParsing(pathname);
    return engine.getFiles();
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

/**
 * Using the default system autodetect mechanism, obtain a
 * list of file information for the current working directory
 * or for just a single file./*www.j  a  va2  s.c o m*/
 * <p>
 * This information is obtained through the LIST command.  The contents of
 * the returned array is determined by the<code> FTPFileEntryParser </code>
 * used.
 * <p>
 * @param pathname  The file or directory to list.  Since the server may
 *                  or may not expand glob expressions, using them here
 *                  is not recommended and may well cause this method to
 *                  fail.
 *                  Also, some servers treat a leading '-' as being an option.
 *                  To avoid this interpretation, use an absolute pathname
 *                  or prefix the pathname with ./ (unix style servers).
 *                  Some servers may support "--" as meaning end of options,
 *                  in which case "-- -xyz" should work.
 *
 * @return The list of file information contained in the given path in
 *         the format determined by the autodetection mechanism
 * @exception FTPConnectionClosedException
 *                   If the FTP server prematurely closes the connection
 *                   as a result of the client being idle or some other
 *                   reason causing the server to send FTP reply code 421.
 *                   This exception may be caught either as an IOException
 *                   or independently as itself.
 * @exception IOException
 *                   If an I/O error occurs while either sending a
 *                   command to the server or receiving a reply
 *                   from the server.
 * @exception org.apache.commons.net.ftp.parser.ParserInitializationException
 *                   Thrown if the parserKey parameter cannot be
 *                   resolved by the selected parser factory.
 *                   In the DefaultFTPEntryParserFactory, this will
 *                   happen when parserKey is neither
 *                   the fully qualified class name of a class
 *                   implementing the interface
 *                   org.apache.commons.net.ftp.FTPFileEntryParser
 *                   nor a string containing one of the recognized keys
 *                   mapping to such a parser or if class loader
 *                   security issues prevent its being loaded.
 * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
 * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
 * @see org.apache.commons.net.ftp.FTPFileEntryParser
 */
public FTPFile[] listFiles(String pathname) throws IOException {
    FTPListParseEngine engine = initiateListParsing((String) null, pathname);
    return engine.getFiles();

}

From source file:org.eclipse.datatools.connectivity.sample.ftp.internal.FtpContentProvider.java

public Object[] getChildren(Object parent) {
    try {/*from  www  . j  a  va 2s.  co m*/
        if (parent instanceof FTPClientObject) {
            FTPClient ftpClient = ((FTPClientObject) parent).getFtpClient();
            if (ftpClient.isConnected()) {
                FTPListParseEngine engine = ftpClient.initiateListParsing();
                FTPFile[] files = engine.getFiles();
                return FTPFileObject.convert(parent, ((FTPClientObject) parent).getProfile(), files);
            }
        } else if (parent instanceof FTPFileObject) {
            FTPFile ftpFile = ((FTPFileObject) parent).getFTPFile();
            FTPClient ftpClient = getFTPClient(parent);
            if (ftpFile.isDirectory() && ftpClient.isConnected()) {
                FTPListParseEngine engine = ftpClient.initiateListParsing(getDirectory((FTPFileObject) parent));
                FTPFile[] files = engine.getFiles();
                return FTPFileObject.convert(parent, ((FTPFileObject) parent).getProfile(), files);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        FTPClient ftpClient = getFTPClient(parent);
        try {
            if (ftpClient != null)
                ftpClient.disconnect();
        } catch (Exception ex) {
        }
    }
    return new Object[0];
}