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:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * Checks to see if the remote file is current as compared with the local
 * file. Returns true if the target file is up to date.
 * @param ftp ftpclient/*from w  ww.  j  a v  a  2  s  . com*/
 * @param localFile local file
 * @param remoteFile remote file
 * @return true if the target file is up to date
 * @throws IOException  in unknown circumstances
 * @throws BuildException if the date of the remote files cannot be found and the action is
 * GET_FILES
 */
protected boolean isUpToDate(FTPClient ftp, File localFile, String remoteFile)
        throws IOException, BuildException {
    task.log("checking date for " + remoteFile, Project.MSG_VERBOSE);

    FTPFile[] files = ftp.listFiles(remoteFile);

    // For Microsoft's Ftp-Service an Array with length 0 is
    // returned if configured to return listings in "MS-DOS"-Format
    if (files == null || files.length == 0) {
        // If we are sending files, then assume out of date.
        // If we are getting files, then throw an error

        if (task.getAction() == FTPTask.SEND_FILES) {
            task.log("Could not date test remote file: " + remoteFile + "assuming out of date.",
                    Project.MSG_VERBOSE);
            return false;
        } else {
            throw new BuildException("could not date test remote file: " + ftp.getReplyString());
        }
    }

    long remoteTimestamp = files[0].getTimestamp().getTime().getTime();
    long localTimestamp = localFile.lastModified();
    long adjustedRemoteTimestamp = remoteTimestamp + task.getTimeDiffMillis() + task.getGranularityMillis();

    StringBuffer msg;
    synchronized (TIMESTAMP_LOGGING_SDF) {
        msg = new StringBuffer("   [").append(TIMESTAMP_LOGGING_SDF.format(new Date(localTimestamp)))
                .append("] local");
    }
    task.log(msg.toString(), Project.MSG_VERBOSE);

    synchronized (TIMESTAMP_LOGGING_SDF) {
        msg = new StringBuffer("   [").append(TIMESTAMP_LOGGING_SDF.format(new Date(adjustedRemoteTimestamp)))
                .append("] remote");
    }
    if (remoteTimestamp != adjustedRemoteTimestamp) {
        synchronized (TIMESTAMP_LOGGING_SDF) {
            msg.append(" - (raw: ").append(TIMESTAMP_LOGGING_SDF.format(new Date(remoteTimestamp))).append(")");
        }
    }
    task.log(msg.toString(), Project.MSG_VERBOSE);

    if (task.getAction() == FTPTask.SEND_FILES) {
        return adjustedRemoteTimestamp >= localTimestamp;
    } else {
        return localTimestamp >= adjustedRemoteTimestamp;
    }
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * Retrieve a single file from the remote host. <code>filename</code> may
 * contain a relative path specification. <p>
 *
 * The file will then be retreived using the entire relative path spec -
 * no attempt is made to change directories. It is anticipated that this
 * may eventually cause problems with some FTP servers, but it simplifies
 * the coding.</p>//from   w  w  w .  j  a  va 2  s  .c  om
 * @param ftp the ftp client
 * @param dir local base directory to which the file should go back
 * @param filename relative path of the file based upon the ftp remote directory
 *        and/or the local base directory (dir)
 * @throws IOException  in unknown circumstances
 * @throws BuildException if skipFailedTransfers is false
 * and the file cannot be retrieved.
 */
protected void getFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException {
    OutputStream outstream = null;
    try {
        File file = task.getProject().resolveFile(new File(dir, filename).getPath());

        if (task.isNewer() && isUpToDate(ftp, file, resolveFile(filename))) {
            return;
        }

        if (task.isVerbose()) {
            task.log("transferring " + filename + " to " + file.getAbsolutePath());
        }

        File pdir = file.getParentFile();

        if (!pdir.exists()) {
            pdir.mkdirs();
        }
        outstream = new BufferedOutputStream(new FileOutputStream(file));
        ftp.retrieveFile(resolveFile(filename), outstream);

        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            String s = "could not get file: " + ftp.getReplyString();

            if (task.isSkipFailedTransfers()) {
                task.log(s, Project.MSG_WARN);
                skipped++;
            } else {
                throw new BuildException(s);
            }

        } else {
            task.log("File " + file.getAbsolutePath() + " copied from " + task.getServer(),
                    Project.MSG_VERBOSE);
            transferred++;
            if (task.isPreserveLastModified()) {
                outstream.close();
                outstream = null;
                FTPFile[] remote = ftp.listFiles(resolveFile(filename));
                if (remote.length > 0) {
                    FILE_UTILS.setFileLastModified(file, remote[0].getTimestamp().getTime().getTime());
                }
            }
        }
    } finally {
        FileUtils.close(outstream);
    }
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * List information about a single file from the remote host. <code>filename</code>
 * may contain a relative path specification. <p>
 *
 * The file listing will then be retrieved using the entire relative path
 * spec - no attempt is made to change directories. It is anticipated that
 * this may eventually cause problems with some FTP servers, but it
 * simplifies the coding.</p>/* www.ja v a 2s  .c  o m*/
 * @param ftp ftp client
 * @param bw buffered writer
 * @param filename the directory one wants to list
 * @throws IOException  in unknown circumstances
 * @throws BuildException in unknown circumstances
 */
protected void listFile(FTPClient ftp, BufferedWriter bw, String filename) throws IOException, BuildException {
    if (task.isVerbose()) {
        task.log("listing " + filename);
    }
    FTPFile[] ftpfiles = ftp.listFiles(resolveFile(filename));

    if (ftpfiles != null && ftpfiles.length > 0) {
        bw.write(ftpfiles[0].toString());
        bw.newLine();
        transferred++;
    }
}

From source file:org.grouter.core.readers.FtpReaderJob.java

@Override
protected List<CommandMessage> readFromSource() {
    logger.info("Reading files from :" + node.getInBound().getUri());

    // a list of full paths on ftp server we will download from
    Map endPointContext = node.getInBound().getEndPointContext();

    List<String> remoteFtpUriToFile = getPathIncludingFile((String) endPointContext.get(FILE_LIST));
    List<CommandMessage> commandMessages = new ArrayList<CommandMessage>();
    FTPClient client = null;
    try {/*from   w  ww .j a v a 2  s.  c o  m*/
        client = initConnection();
        for (String fullPathToFile : remoteFtpUriToFile) {
            // should only return one file - since we are using a complete file uri and not a uri to a folder
            FTPFile[] ftpFilesAtPath = client.listFiles(fullPathToFile);
            if (ftpFilesAtPath.length > 0) {
                //String localFileName = fullPathToFile;
                File internalInFile = new File(node.getRouter().getHomePath() + File.separator + "nodes"
                        + File.separator + node.getId() + File.separator + "internal" + File.separator + "in"
                        + File.separator + fullPathToFile.replace("/", "_"));
                FileOutputStream fos = new FileOutputStream(internalInFile);
                logger.info("Downloading file from ftp server:" + fullPathToFile);
                // we have a valid fullPathToFile and there is a file at that fullPathToFile
                boolean status = client.retrieveFile(fullPathToFile, fos);
                if (status) {
                    logger.info("Downloading complete :" + internalInFile);
                    internalInFile.setLastModified(ftpFilesAtPath[0].getTimestamp().getTimeInMillis());

                    // Get part of the message to store for querying purposes
                    String message = getMessage(internalInFile);
                    CommandMessage cmdMessage = new CommandMessage(message, internalInFile);
                    commandMessages.add(cmdMessage);
                } else {
                    logger.error("Failed to download remote file :" + fullPathToFile + " Status code received :"
                            + status);
                }
                fos.close();
            }
        }
    } catch (Exception e) {
        // TODO We need to reset state if we start working again
        node.setNodeStatus(NodeStatus.ERROR);
        node.setStatusMessage(
                "Failed reading files from :" + node.getInBound().getUri() + " Error:" + e.getMessage());
        logStrategy.log(node);
        logger.warn("Connection problem with FTP server.", e);
    } finally {
        if (client != null) {
            try {
                client.logout();
                client.disconnect();
            } catch (IOException e) {
                //ignore
            }
        }
    }
    return commandMessages;
}

From source file:org.jason.mapmaker.server.service.ShapefileMetadataServiceImpl.java

private List<String> getRemoteFilenames(String url, String directory) {

    FTPClient ftp = new FTPClient();
    List<String> filenameList = new ArrayList<String>();

    try {/*from  w  ww . j  a v a  2 s.  c o m*/
        int reply;
        ftp.connect(url);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.out.println("FTP connection failed for " + url);
        }
        ftp.enterLocalPassiveMode();
        ftp.login("anonymous", "");
        FTPFile[] files = ftp.listFiles(directory);
        for (FTPFile f : files) {
            filenameList.add(f.getName());
        }

        ftp.logout();

    } catch (IOException ex) {

        ex.printStackTrace();
    }

    return filenameList;
}

From source file:org.jevis.commons.driver.DataSourceHelper.java

private static List<String> getMatchingPathes(String path, String[] pathStream, ArrayList<String> arrayList,
        FTPClient fc, DateTime lastReadout, DateTimeFormatterBuilder dtfbuilder) {
    int nextTokenPos = getPathTokens(path).length;
    if (nextTokenPos == pathStream.length - 1) {
        arrayList.add(path);/*  ww w . j ava  2  s.  c o m*/
        return arrayList;
    }

    String nextToken = pathStream[nextTokenPos];
    String nextFolder = null;

    try {
        if (containsDateToken(nextToken)) {
            FTPFile[] listDirectories = fc.listFiles(path);
            //                DateTimeFormatter ftmTemp = getDateFormatter(nextToken);
            for (FTPFile folder : listDirectories) {
                if (!matchDateString(folder.getName(), nextToken)) {
                    continue;
                }
                //                    System.out.println("listdir," + folder.getName());
                //                    if (containsDate(folder.getName(), ftmTemp)) {
                DateTime folderTime = getFolderTime(path + folder.getName() + "/", pathStream);
                if (folderTime.isAfter(lastReadout)) {
                    nextFolder = folder.getName();
                    //                        System.out.println("dateFolder," + nextFolder);
                    getMatchingPathes(path + nextFolder + "/", pathStream, arrayList, fc, lastReadout,
                            dtfbuilder);
                }
                //                    }
            }
        } else {
            nextFolder = nextToken;
            //                System.out.println("normalFolder," + nextFolder);
            getMatchingPathes(path + nextFolder + "/", pathStream, arrayList, fc, lastReadout, dtfbuilder);
        }
    } catch (IOException ex) {
        org.apache.log4j.Logger.getLogger(DataSourceHelper.class).log(org.apache.log4j.Level.ERROR,
                ex.getMessage());
    }
    return arrayList;
}

From source file:org.moxie.ftp.FTPTaskMirrorImpl.java

/**
 * auto find the time difference between local and remote
 * @param ftp handle to ftp client// w  ww. j  a  v a 2 s  .  c o m
 * @return number of millis to add to remote time to make it comparable to local time
 * @since ant 1.6
 */
private long getTimeDiff(FTPClient ftp) {
    long returnValue = 0;
    File tempFile = findFileName(ftp);
    try {
        // create a local temporary file
        FILE_UTILS.createNewFile(tempFile);
        long localTimeStamp = tempFile.lastModified();
        BufferedInputStream instream = new BufferedInputStream(new FileInputStream(tempFile));
        ftp.storeFile(tempFile.getName(), instream);
        instream.close();
        boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());
        if (success) {
            FTPFile[] ftpFiles = ftp.listFiles(tempFile.getName());
            if (ftpFiles.length == 1) {
                long remoteTimeStamp = ftpFiles[0].getTimestamp().getTime().getTime();
                returnValue = localTimeStamp - remoteTimeStamp;
            }
            ftp.deleteFile(ftpFiles[0].getName());
        }
        // delegate the deletion of the local temp file to the delete task
        // because of race conditions occuring on Windows
        Delete mydelete = new Delete();
        mydelete.bindToOwner(task);
        mydelete.setFile(tempFile.getCanonicalFile());
        mydelete.execute();
    } catch (Exception e) {
        throw new BuildException(e, task.getLocation());
    }
    return returnValue;
}

From source file:org.moxie.ftp.FTPTaskMirrorImpl.java

/**
 * Retrieve a single file from the remote host. <code>filename</code> may
 * contain a relative path specification. <p>
 *
 * The file will then be retreived using the entire relative path spec -
 * no attempt is made to change directories. It is anticipated that this
 * may eventually cause problems with some FTP servers, but it simplifies
 * the coding.</p>//from  w  w  w  . j  a  v  a  2 s .  c om
 * @param ftp the ftp client
 * @param dir local base directory to which the file should go back
 * @param filename relative path of the file based upon the ftp remote directory
 *        and/or the local base directory (dir)
 * @throws IOException  in unknown circumstances
 * @throws BuildException if skipFailedTransfers is false
 * and the file cannot be retrieved.
 */
protected void getFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException {
    OutputStream outstream = null;
    try {
        File file = task.getProject().resolveFile(new File(dir, filename).getPath());

        if (task.isNewer() && isUpToDate(ftp, file, resolveFile(filename))) {
            return;
        }

        if (task.isVerbose()) {
            task.log("transferring " + filename + " to " + file.getAbsolutePath());
        }

        File pdir = file.getParentFile();

        if (!pdir.exists()) {
            pdir.mkdirs();
        }
        outstream = new BufferedOutputStream(new FileOutputStream(file));
        ftp.retrieveFile(resolveFile(filename), outstream);

        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            String s = "could not get file: " + ftp.getReplyString();

            if (task.isSkipFailedTransfers()) {
                task.log(s, Project.MSG_WARN);
                skipped++;
            } else {
                throw new BuildException(s);
            }

        } else {
            task.log("File " + file.getAbsolutePath() + " copied from " + task.getServer(),
                    Project.MSG_VERBOSE);
            transferred++;
            if (task.isPreserveLastModified()) {
                outstream.close();
                outstream = null;
                FTPFile[] remote = ftp.listFiles(resolveFile(filename));
                if (remote.length > 0) {
                    FILE_UTILS.setFileLastModified(file, remote[0].getTimestamp().getTime().getTime());
                }
            }
        }
    } finally {
        if (outstream != null) {
            try {
                outstream.close();
            } catch (IOException ex) {
                // ignore it
            }
        }
    }
}

From source file:org.mule.modules.FtpUtils.java

public static FTPFile[] listFiles(FTPClient client, String path) {
    try {// ww w.j  a va2s  .co m
        if (path == null || path.isEmpty()) {
            path = client.printWorkingDirectory();
        }
        return client.listFiles(path);
    } catch (IOException e) {
        try {
            client.disconnect();
        } catch (Exception error) {
            throw new FtpLiteException("There was an error disconnecting. " + error.toString());
        }
        throw new FtpLiteException("There was an error fetching files from SFTP");
    }
}

From source file:org.mule.modules.FtpUtils.java

public static boolean fileExists(FTPClient client, String filePath, String fileName) {
    try {// w  ww  .jav a2s.c  om
        String fullPath = createFullPath(filePath, fileName);
        client.changeWorkingDirectory(filePath);
        if (client.listFiles(fileName).length == 1) {
            return true;
        } else {
            return false;
        }
    } catch (IOException e) {
        throw new FtpLiteException("Error looking up the file");
    }
}