Example usage for org.apache.commons.net.ftp FTPFile getName

List of usage examples for org.apache.commons.net.ftp FTPFile getName

Introduction

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

Prototype

public String getName() 

Source Link

Document

Return the name of the file.

Usage

From source file:org.runmyprocess.sec.FTP.java

private void listFiles(JSONObject jsonObject) throws Exception {
    // get all files from server and store them in an array of
    // FTPFiles/*from www.  j av  a 2s .  c  o m*/
    JSONArray fileInfo = new JSONArray();
    LOG.log("LIST directories: " + jsonObject.getString("path"), Level.INFO);
    String[] directories = this.client.listNames(jsonObject.getString("path"));
    LOG.log("LIST files: " + jsonObject.getString("path"), Level.INFO);
    FTPFile[] files = this.client.listFiles(jsonObject.getString("path"));
    LOG.log("LIST DONE", Level.INFO);

    for (FTPFile file : files) {
        if (file.getType() == FTPFile.FILE_TYPE) {
            JSONObject fileObject = new JSONObject();
            fileObject.put("Name", file.getName());
            fileObject.put("Size", FileUtils.byteCountToDisplaySize(file.getSize()));
            fileObject.put("Timestamp", file.getTimestamp().getTime().toString());
            fileInfo.add(fileObject);
        }
    }
    response.setStatus(200);//sets the return status to 200
    JSONObject resp = new JSONObject();
    resp.put("files", fileInfo);//sends the info inside an object
    resp.put("directories", directories);
    response.setData(resp);
}

From source file:org.schedoscope.export.ftp.FtpExportCSVMRTest.java

private int getFileCount() throws IOException {

    FTPClient ftp = new FTPClient();
    ftp.connect("localhost", 2221);
    ftp.login(EmbeddedFtpSftpServer.FTP_USER_FOR_TESTING, EmbeddedFtpSftpServer.FTP_PASS_FOR_TESTING);
    FTPFile[] files = ftp.listFiles();/*from  w  ww.  ja  va 2 s .  co m*/

    int fileCounter = 0;
    for (FTPFile f : files) {
        if (f.getName().contains(filePrefix)) {
            fileCounter += 1;
        }
    }
    return fileCounter;
}

From source file:org.shept.util.FtpFileCopy.java

/**
 * Compare the source path and the destination path for filenames with the filePattern
 * e.g. *.bak, *tmp and copy these files to the destination dir if they are not present at the
 * destination or if they have changed (by modifactionDate).
 * Copying is done through a retrieve operation.
 * //from w w  w .j a  va2s .  c o  m
 * @param ftpSource
 * @param localDestPat
 * @param filePattern
 * @return the number of files being copied
 * @throws IOException 
 */
public Integer syncPull(FTPClient ftpSource, String localDestPat, String filePattern) throws IOException {
    // check for new files since the last check which need to be copied

    Integer number = 0;
    SortedMap<FileNameDate, File> destMap = FileUtils.fileMapByNameAndDate(localDestPat, filePattern);
    SortedMap<FileNameDate, FTPFile> sourceMap = fileMapByNameAndDate(ftpSource, filePattern);
    // identify the list of source files different from their destinations
    for (FileNameDate fk : destMap.keySet()) {
        sourceMap.remove(fk);
    }

    // copy the list of files from source to destination
    for (FTPFile file : sourceMap.values()) {
        logger.debug("Copying file " + file.getName() + ": " + file.getTimestamp());
        File copy = new File(localDestPat, file.getName());
        try {
            if (!copy.exists()) {
                // copy to tmp file first to avoid clashes during lengthy copy action
                File tmp = File.createTempFile("vrs", ".tmp", copy.getParentFile());
                FileOutputStream writeStream = new FileOutputStream(tmp);
                boolean rc = ftpSource.retrieveFile(file.getName(), writeStream);
                writeStream.close();
                if (rc) {
                    rc = tmp.renameTo(copy);
                    number++;
                }
                if (!rc) {
                    tmp.delete(); // cleanup if we fail
                }
            }
        } catch (IOException ex) {
            logger.error("Ftp FileCopy did not succeed (using " + file.getName() + ")" + " FTP reported error  "
                    + ftpSource.getReplyString(), ex);
        }
    }
    return number;
}

From source file:org.shept.util.FtpFileCopy.java

/**
 * List the file directory as specified by the name filter and the path directory
 * The maps keys contain name and modification date for comparison
 * @throws IOException //from   ww w .  j  av  a 2  s .c  om
 */
public SortedMap<FileNameDate, FTPFile> fileMapByNameAndDate(FTPClient ftp, String filePattern)
        throws IOException {
    SortedMap<FileNameDate, FTPFile> newFileMap = new TreeMap<FileNameDate, FTPFile>();
    FTPFile[] files = ftp.listFiles();
    for (FTPFile ftpFile : files) {
        if (acceptFile(ftpFile, filePattern)) {
            ftpFile.getTimestamp();
            FileNameDate fk = new FileNameDate(ftpFile.getName(), ftpFile.getTimestamp().getTimeInMillis());
            newFileMap.put(fk, ftpFile);
        }
    }
    return newFileMap;
}

From source file:org.shept.util.FtpFileCopy.java

protected boolean acceptFile(FTPFile file, String pattern) {
    if (file.isDirectory())
        return false;
    return PatternMatchUtils.simpleMatch(pattern, file.getName());
}

From source file:org.sipfoundry.sipxconfig.admin.ftp.FtpContextImpl.java

private String[] listByType(String path, Integer type) {
    List<String> names = new ArrayList<String>();
    try {/*from  w w  w .ja v a2s .  c o  m*/
        FTPFile[] files = m_client.listFiles(path);
        for (FTPFile file : files) {
            if (file == null) {
                continue;
            }
            if (type == null || type.intValue() == file.getType()) {
                names.add(file.getName());
            }
        }
    } catch (IOException e) {
        LOG.error(e);
        throw new UserException(INTERNAL_FTP_ERROR);
    }
    return names.toArray(new String[names.size()]);
}

From source file:org.sipfoundry.sipxconfig.admin.ftp.FtpContextImpl.java

private FTPFile[] searchFiles(String... names) {
    try {/*from w  w w .  j a  v a  2 s  .co  m*/
        FTPFile[] allFiles = m_client.listFiles(".");
        List<FTPFile> filesToSearch = new ArrayList<FTPFile>();
        for (FTPFile file : allFiles) {
            if (ArrayUtils.contains(names, file.getName())) {
                filesToSearch.add(file);
            }
        }

        return filesToSearch.toArray(new FTPFile[0]);
    } catch (IOException e) {
        LOG.error(e);
        throw new UserException(INTERNAL_FTP_ERROR);
    }
}

From source file:org.sipfoundry.sipxconfig.admin.ftp.FtpContextImpl.java

private void delete(FTPFile... files) {
    try {//from w  w  w  .  jav a2  s  .c om
        for (FTPFile file : files) {
            if (file.isFile()) {
                m_client.deleteFile(file.getName());
            } else if (file.isDirectory()) {
                changeDirectory(file.getName());
                delete(m_client.listFiles());
                changeDirectory(UP);
                m_client.removeDirectory(file.getName());
            }
        }

    } catch (IOException e) {
        LOG.error(e);
        throw new UserException(INTERNAL_FTP_ERROR);
    }
}

From source file:org.sipfoundry.sipxconfig.admin.ftp.FtpContextImpl.java

private void download(String locationPath, FTPFile... files) {
    try {//from ww  w  .j ava2  s  .  c o  m
        for (FTPFile file : files) {
            if (file.isFile()) {
                OutputStream output = new FileOutputStream(locationPath + File.separator + file.getName());
                m_client.retrieveFile(file.getName(), output);
                output.close();
            } else if (file.isDirectory()) {
                changeDirectory(file.getName());
                File fileNew = new File(locationPath + File.separator + file.getName());
                fileNew.mkdir();
                download(locationPath + File.separator + file.getName(), m_client.listFiles());
                changeDirectory(UP);
            }
        }

    } catch (IOException e) {
        LOG.error(e);
        throw new UserException(INTERNAL_FTP_ERROR);
    }
}

From source file:org.sofun.platform.opta.ftp.FTPFileFilterImpl.java

@Override
public boolean accept(FTPFile file) {
    if (file == null || filePattern == null) {
        return false;
    }/* w w w.  ja va2 s .  c  o m*/
    final Pattern p = Pattern.compile(filePattern);
    final Matcher m = p.matcher(file.getName());
    if (!m.find()) {
        // Filename does not match pattern. Do not include.
        return false;
    }
    if (service == null) {
        // No service available to filter further. Filename matches pattern:
        // do include.
        return true;
    }
    OptaProcessedFeed pfeed = service.getProcessedFeedFor(file.getName());
    if (pfeed == null) {
        // File has not been processed yet: do include
        return true;
    } else {
        Date formerTimestamp = pfeed.getTimestamp();
        if (formerTimestamp == null) {
            // No record of last processing: do include.
            return true;
        }
        if (formerTimestamp.compareTo(file.getTimestamp().getTime()) < 0) {
            // Former time stamp earlier than new one: do include
            return true;
        }
    }
    return false;
}