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(String pathname) throws IOException 

Source Link

Document

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

Usage

From source file:com.microsoft.intellij.util.WAHelper.java

public static boolean checkFileCountOnFTPServer(FTPClient ftp, String path, int fileCount) throws IOException {
    boolean fileExtracted = false;
    FTPFile[] files = ftp.listFiles(path);
    if (files.length >= fileCount) {
        fileExtracted = true;// w w w .jav a 2 s . c o  m
    }
    return fileExtracted;
}

From source file:com.hdfstoftp.main.HdfsToFtp.java

/**
 * ??Map/*from w w  w  .  j  a v  a2s. c o m*/
 * 
 * @param path
 * @return Map<String, String>
 * @throws Exception
 * @throws IllegalStateException
 * @throws NoSuchElementException
 */
public static Map<String, String> getFileNameMap(String path, FTPClientPool ftpPool)
        throws NoSuchElementException, IllegalStateException, Exception {
    Map<String, String> fileNameMap = new HashMap<String, String>();
    FTPClient client = ftpPool.borrowObject();
    FTPFile[] files = client.listFiles(path);
    ftpPool.returnObject(client);
    for (FTPFile file : files) {
        if (file.isFile()) {
            fileNameMap.put(file.getName(), "");
        }
    }
    return fileNameMap;
}

From source file:com.ephesoft.dcma.util.FTPUtil.java

/**
 * API for deleting the data existing in ftp directory on the ftp server.
 * //ww  w .ja va  2 s .c o  m
 * @param client {@link FTPClient}
 * @param ftpDirectory {@link String}
 * @param isSourceFolderDeleted
 */
public static void deleteExistingFTPData(final FTPClient client, final String ftpDirectory,
        final boolean isSourceFolderDeleted) {
    if (client != null && ftpDirectory != null) {
        try {
            FTPFile[] ftpFileList = client.listFiles(ftpDirectory);
            for (FTPFile ftpFile : ftpFileList) {
                client.deleteFile(
                        EphesoftStringUtil.concatenate(ftpDirectory, File.separator, ftpFile.getName()));
            }
            if (isSourceFolderDeleted) {
                client.removeDirectory(ftpDirectory);
            }
        } catch (IOException e) {
            LOGGER.error(EphesoftStringUtil.concatenate("Error in deleting existing file on ftp server ",
                    e.getMessage(), e));
        }
    }
}

From source file:com.zurich.lifeac.intra.control.FTPUtil.java

public static void downloadDirectory(FTPClient ftpClient, String parentDir, String currentDir, String saveDir)
        throws IOException {//parentDir=remote_edir ,saveDir=loc_dir
    String dirToList = parentDir;

    if (!currentDir.equals("")) {
        dirToList += "/" + currentDir;
    }/* w  w  w  .ja  va  2 s .com*/
    System.out.println("downloadDirectory >dirToList:" + dirToList);
    FTPFile[] subFiles = ftpClient.listFiles(dirToList);

    if (subFiles != null && subFiles.length > 0) {
        for (FTPFile aFile : subFiles) {
            String currentFileName = aFile.getName();
            if (currentFileName.equals(".") || currentFileName.equals("..")) {
                // skip parent directory and the directory itself
                continue;
            }
            String filePath = parentDir + "/" + currentDir + "/" + currentFileName;

            if (currentDir.equals("")) {
                filePath = parentDir + "/" + currentFileName;
            }
            //System.out.println(filePath);
            String newDirPath = saveDir + File.separator + currentDir + File.separator + currentFileName;
            if (currentDir.equals("")) {
                newDirPath = saveDir + File.separator + currentFileName;
            }

            if (aFile.isDirectory()) {
                // create the directory in saveDir
                //                    File newDir = new File(newDirPath);
                //                    boolean created = newDir.mkdirs();
                //                    if (created) {
                //                        System.out.println("CREATED the directory: " + newDirPath);
                //                    } else {
                //                        System.out.println("COULD NOT create the directory: " + newDirPath);
                //                    }
                //
                //                    // download the sub directory
                //                    downloadDirectory(ftpClient, dirToList, currentFileName,
                //                            saveDir);
            } else {
                // download the file
                System.out.println("DOWNLOADED the file: " + filePath);
                boolean success = downloadSingleFile(ftpClient, filePath, newDirPath);
                if (success) {

                    //if download success then delete the file.
                    boolean deleted = ftpClient.deleteFile(filePath);
                    if (deleted) {
                        System.out.println("delete the file: " + filePath);
                    } else {
                        System.out.println("COULD NOT delete the file: " + filePath);
                    }

                }
            }
        }
    } else {
        System.out.println("There are no .txt file");
    }
}

From source file:com.jaspersoft.jasperserver.api.engine.common.util.impl.FTPUtil.java

private static boolean exists(FTPClient ftpClient, String fileName) throws Exception {
    if (ftpClient == null)
        throw new JSException("Please connect to FTP server first before changing directory!");
    try {/*from   w w w . j  a v a2s  .  c om*/
        if (log.isDebugEnabled())
            log.debug("FTP Working directory = " + ftpClient.printWorkingDirectory());
        FTPFile[] files = ftpClient.listFiles(ftpClient.printWorkingDirectory());

        if (log.isDebugEnabled())
            log.debug("FTP:  number of files - " + (files == null ? "NULL" : files.length));

        if (files == null)
            return false;
        for (FTPFile ftpFile : files) {
            if (ftpFile.getName().equalsIgnoreCase(fileName))
                return true;
        }
        return false;
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
}

From source file:com.seajas.search.contender.service.modifier.ArchiveModifierService.java

/**
 * Recursively retrieve a directory listing from the FTP server.
 * // ww  w  .  jav a  2s  .  c o m
 * @param path
 * @param ftpClient
 * @param exclusionExpression
 * @return List<FTPFile>
 * @throws IOException
 */
private static List<ArchiveFile> retrieveFiles(final String path, final FTPClient ftpClient,
        final String exclusionExpression) throws IOException {
    List<FTPFile> files = Arrays.asList(ftpClient.listFiles(path));
    List<ArchiveFile> result = new Vector<ArchiveFile>();

    for (FTPFile file : files)
        if (!file.getName().equals(".") && !file.getName().equals("..")) {
            if (file.isDirectory()) {
                String folderPath = path + (path.endsWith("/") ? "" : "/") + file.getName();

                if (!StringUtils.hasText(exclusionExpression) || !folderPath.matches(exclusionExpression))
                    result.addAll(retrieveFiles(folderPath, ftpClient, exclusionExpression));
            } else if (file.isFile() && (!StringUtils.hasText(exclusionExpression)
                    || !(path + "/" + file.getName()).matches(exclusionExpression)))
                result.add(new ArchiveFile(path, file));
        }

    return result;
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

public static boolean doesRemoteFileExist(FTPClient ftp, String path, String fileName) throws IOException {
    FTPFile[] files = ftp.listFiles(path);
    for (FTPFile file : files) {
        if (file.isFile() && file.getName().equalsIgnoreCase(fileName)) {
            return true;
        }//from   w ww .j a v  a2 s  .com
    }
    return false;
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

public static boolean doesRemoteFolderExist(FTPClient ftp, String path, String folderName) throws IOException {
    FTPFile[] files = ftp.listFiles(path);
    for (FTPFile file : files) {
        if (file.isDirectory() && file.getName().equalsIgnoreCase(folderName)) {
            return true;
        }//from   w  w w. ja  va  2s.  c om
    }
    return false;
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

public static void removeFtpDirectory(FTPClient ftpClient, String path, IProgressIndicator pi)
        throws IOException {
    String prefix = "Removing from FTP server: ";
    FTPFile[] subFiles = ftpClient.listFiles(path);
    if (subFiles.length > 0) {
        for (FTPFile ftpFile : subFiles) {
            if (pi != null && pi.isCanceled())
                break;
            String currentFileName = ftpFile.getName();
            if (currentFileName.equals(".") || currentFileName.equals("..")) {
                continue; // skip
            }//from w ww.j av a2s .c  om

            String path1 = path + "/" + currentFileName;
            if (ftpFile.isDirectory()) {
                // remove the sub directory
                removeFtpDirectory(ftpClient, path1, pi);
            } else {
                // delete the file
                if (pi != null)
                    pi.setText2(prefix + path1);
                ftpClient.deleteFile(path1);
            }
        }
    }

    if (pi != null)
        pi.setText2(prefix + path);
    ftpClient.removeDirectory(path);
    if (pi != null)
        pi.setText2("");
}

From source file:com.mendhak.gpslogger.senders.ftp.Ftp.java

public static boolean Upload(String server, String username, String password, int port, boolean useFtps,
        String protocol, boolean implicit, InputStream inputStream, String fileName) {
    FTPClient client = null;

    try {//from  ww w.  j av a 2s . c  o  m
        if (useFtps) {
            client = new FTPSClient(protocol, implicit);

            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(null, null);
            KeyManager km = kmf.getKeyManagers()[0];
            ((FTPSClient) client).setKeyManager(km);
        } else {
            client = new FTPClient();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        Utilities.LogDebug("Connecting to FTP");
        client.connect(server, port);

        Utilities.LogDebug("Logging in to FTP server");
        if (client.login(username, password)) {
            client.enterLocalPassiveMode();

            Utilities.LogDebug("Uploading file to FTP server");

            FTPFile[] existingDirectory = client.listFiles("GPSLogger");

            if (existingDirectory.length <= 0) {
                client.makeDirectory("GPSLogger");
            }

            client.changeWorkingDirectory("GPSLogger");
            boolean result = client.storeFile(fileName, inputStream);
            inputStream.close();
            if (result) {
                Utilities.LogDebug("Successfully FTPd file");
            } else {
                Utilities.LogDebug("Failed to FTP file");
            }

        } else {
            Utilities.LogDebug("Could not log in to FTP server");
            return false;
        }

    } catch (Exception e) {
        Utilities.LogError("Could not connect or upload to FTP server.", e);
    } finally {
        try {
            Utilities.LogDebug("Logging out of FTP server");
            client.logout();

            Utilities.LogDebug("Disconnecting from FTP server");
            client.disconnect();
        } catch (Exception e) {
            Utilities.LogError("Could not logout or disconnect", e);
        }
    }

    return true;
}