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, FTPFileFilter filter) throws IOException 

Source Link

Document

Version of #listFiles(String) which allows a filter to be provided.

Usage

From source file:de.ep3.ftpc.model.CrawlerDirectory.java

public CrawlerDirectory(FTPClient ftpClient, String path) throws IOException {
    files = ftpClient.listFiles(path, this);
    position = 0;//  www  . j  a  v a 2 s.co m

    this.path = path;
}

From source file:com.jaeksoft.searchlib.crawler.file.process.fileInstances.FtpFileInstance.java

@Override
public FileInstanceAbstract[] listFilesOnly() throws SearchLibException {
    FTPClient f = null;
    try {/*from  www .ja  va  2  s.c  o  m*/
        f = ftpConnect();
        FTPFile[] files = f.listFiles(getPath(), new FtpInstanceFileFilter(filePathItem.isIgnoreHiddenFiles(),
                true, filePathItem.getExclusionMatchers()));
        return buildFileInstanceArray(files);
    } catch (SocketException e) {
        throw new SearchLibException(e);
    } catch (IOException e) {
        throw new SearchLibException(e);
    } catch (URISyntaxException e) {
        throw new SearchLibException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SearchLibException(e);
    } finally {
        ftpQuietDisconnect(f);
    }
}

From source file:com.jaeksoft.searchlib.crawler.file.process.fileInstances.FtpFileInstance.java

@Override
public FileInstanceAbstract[] listFilesAndDirectories() throws SearchLibException {
    FTPClient f = null;
    try {//from w  w w.  j  a v a 2 s  .  co  m
        f = ftpConnect();
        FTPFile[] files = f.listFiles(getPath(), new FtpInstanceFileFilter(filePathItem.isIgnoreHiddenFiles(),
                false, filePathItem.getExclusionMatchers()));
        return buildFileInstanceArray(files);
    } catch (SocketException e) {
        throw new SearchLibException(e);
    } catch (IOException e) {
        throw new SearchLibException(e);
    } catch (URISyntaxException e) {
        throw new SearchLibException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SearchLibException(e);
    } finally {
        ftpQuietDisconnect(f);
    }
}

From source file:com.jaeksoft.searchlib.scheduler.task.TaskFtpXmlFeed.java

@Override
public void execute(Client client, TaskProperties properties, Variables variables, TaskLog taskLog)
        throws SearchLibException {
    String server = properties.getValue(propServer);
    String path = properties.getValue(propPath);
    String login = properties.getValue(propLogin);
    String password = properties.getValue(propPassword);
    String fileNamePattern = properties.getValue(propFileNamePattern);
    boolean deleteAfterLoad = Boolean.TRUE.toString().equals(properties.getValue(propDeleteAfterLoad));
    boolean truncateWhenFilesFound = Boolean.TRUE.toString()
            .equals(properties.getValue(propTruncateIndexWhenFilesFound));
    Pattern pattern = null;//from w w w . j a  va2 s . co m
    if (fileNamePattern != null && fileNamePattern.length() > 0)
        pattern = Pattern.compile(fileNamePattern);

    String p = properties.getValue(propBuffersize);
    String xsl = properties.getValue(propXsl);
    File xmlTempResult = null;
    int bufferSize = 50;
    if (p != null && p.length() > 0)
        bufferSize = Integer.parseInt(p);
    HttpDownloader httpDownloader = client.getWebCrawlMaster().getNewHttpDownloader(true);
    FTPClient ftp = null;
    InputStream inputStream = null;
    try {
        // FTP Connection
        ftp = new FTPClient();
        checkConnect(ftp, server, login, password);
        FTPFile[] files = ftp.listFiles(path, new FtpFileInstance.FtpInstanceFileFilter(true, false, null));
        if (files == null)
            return;
        // Sort by ascendant filename
        String[] fileNames = new String[files.length];
        int i = 0;
        for (FTPFile file : files)
            fileNames[i++] = file.getName();
        Arrays.sort(fileNames);
        int ignored = 0;
        int loaded = 0;
        boolean bAlreadyTruncated = false;
        for (String fileName : fileNames) {
            String filePathName = FilenameUtils.concat(path, fileName);
            if (pattern != null)
                if (!pattern.matcher(fileName).find()) {
                    ignored++;
                    continue;
                }
            if (truncateWhenFilesFound && !bAlreadyTruncated) {
                client.deleteAll();
                bAlreadyTruncated = true;
            }
            taskLog.setInfo("Working on: " + filePathName);
            inputStream = ftp.retrieveFileStream(filePathName);
            Node xmlDoc = null;
            if (xsl != null && xsl.length() > 0) {
                xmlTempResult = File.createTempFile("ossftpfeed", ".xml");
                DomUtils.xslt(new StreamSource(inputStream), xsl, xmlTempResult);
                xmlDoc = DomUtils.readXml(new StreamSource(xmlTempResult), false);
            } else
                xmlDoc = DomUtils.readXml(new StreamSource(inputStream), false);
            client.updateXmlDocuments(xmlDoc, bufferSize, null, httpDownloader, taskLog);
            client.deleteXmlDocuments(xmlDoc, bufferSize, taskLog);
            inputStream.close();
            inputStream = null;
            if (!ftp.completePendingCommand())
                throw new SearchLibException("FTP Error");
            if (xmlTempResult != null) {
                xmlTempResult.delete();
                xmlTempResult = null;
            }
            checkConnect(ftp, server, login, password);
            if (deleteAfterLoad)
                ftp.deleteFile(filePathName);
            loaded++;
        }
        taskLog.setInfo(loaded + " file(s) loaded - " + ignored + " file(s) ignored");
    } catch (XPathExpressionException e) {
        throw new SearchLibException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SearchLibException(e);
    } catch (ParserConfigurationException e) {
        throw new SearchLibException(e);
    } catch (SAXException e) {
        throw new SearchLibException(e);
    } catch (IOException e) {
        throw new SearchLibException(e);
    } catch (URISyntaxException e) {
        throw new SearchLibException(e);
    } catch (InstantiationException e) {
        throw new SearchLibException(e);
    } catch (IllegalAccessException e) {
        throw new SearchLibException(e);
    } catch (ClassNotFoundException e) {
        throw new SearchLibException(e);
    } catch (TransformerException e) {
        throw new SearchLibException(e);
    } finally {
        if (xmlTempResult != null)
            xmlTempResult.delete();
        IOUtils.close(inputStream);
        try {
            if (ftp != null)
                if (ftp.isConnected())
                    ftp.disconnect();
        } catch (IOException e) {
            Logging.warn(e);
        }
        if (httpDownloader != null)
            httpDownloader.release();
    }
}

From source file:com.peter.javaautoupdater.JavaAutoUpdater.java

public static void run(String ftpHost, int ftpPort, String ftpUsername, String ftpPassword,
        boolean isLocalPassiveMode, boolean isRemotePassiveMode, String basePath, String softwareName,
        String args[]) {//from  ww  w . j av  a  2s .  com
    System.out.println("jarName=" + jarName);
    for (String arg : args) {
        if (arg.toLowerCase().trim().equals("-noautoupdate")) {
            return;
        }
    }
    JavaAutoUpdater.basePath = basePath;
    JavaAutoUpdater.softwareName = softwareName;
    JavaAutoUpdater.args = args;

    if (!jarName.endsWith(".jar") || jarName.startsWith("JavaAutoUpdater-")) {
        if (isDebug) {
            jarName = "test.jar";
        } else {
            return;
        }
    }
    JProgressBarDialog d = new JProgressBarDialog(new JFrame(), "Auto updater", true);

    d.progressBar.setIndeterminate(true);
    d.progressBar.setStringPainted(true);
    d.progressBar.setString("Updating");
    //      d.addCancelEventListener(this);
    Thread longRunningThread = new Thread() {
        public void run() {
            d.progressBar.setString("checking latest version");
            System.out.println("checking latest version");

            FTPClient ftp = new FTPClient();
            try {
                ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
                ftp.connect(ftpHost, ftpPort);
                int reply = ftp.getReplyCode();
                System.out.println("reply=" + reply + ", " + FTPReply.isPositiveCompletion(reply));
                if (!FTPReply.isPositiveCompletion(reply)) {
                    ftp.disconnect();
                    JOptionPane.showMessageDialog(null, "FTP server refused connection", "error",
                            JOptionPane.ERROR_MESSAGE);
                }
                d.progressBar.setString("connected to ftp");
                System.out.println("connected to ftp");
                boolean success = ftp.login(ftpUsername, ftpPassword);
                if (!success) {
                    ftp.disconnect();
                    JOptionPane.showMessageDialog(null, "FTP login fail, can't update software", "error",
                            JOptionPane.ERROR_MESSAGE);
                }
                if (isLocalPassiveMode) {
                    ftp.enterLocalPassiveMode();
                }
                if (isRemotePassiveMode) {
                    ftp.enterRemotePassiveMode();
                }
                FTPFile[] ftpFiles = ftp.listFiles(basePath, new FTPFileFilter() {
                    @Override
                    public boolean accept(FTPFile file) {
                        if (file.getName().startsWith(softwareName)) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                });
                if (ftpFiles.length > 0) {
                    FTPFile targetFile = ftpFiles[ftpFiles.length - 1];
                    System.out.println("targetFile : " + targetFile.getName() + " , " + targetFile.getSize()
                            + "!=" + new File(jarName).length());
                    if (!targetFile.getName().equals(jarName)
                            || targetFile.getSize() != new File(jarName).length()) {
                        int r = JOptionPane.showConfirmDialog(null,
                                "Confirm to update to " + targetFile.getName(), "Question",
                                JOptionPane.YES_NO_OPTION);
                        if (r == JOptionPane.YES_OPTION) {
                            //ftp.enterRemotePassiveMode();
                            d.progressBar.setString("downloading " + targetFile.getName());
                            ftp.setFileType(FTP.BINARY_FILE_TYPE);
                            ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);

                            d.progressBar.setIndeterminate(false);
                            d.progressBar.setMaximum(100);
                            CopyStreamAdapter streamListener = new CopyStreamAdapter() {

                                @Override
                                public void bytesTransferred(long totalBytesTransferred, int bytesTransferred,
                                        long streamSize) {
                                    int percent = (int) (totalBytesTransferred * 100 / targetFile.getSize());
                                    d.progressBar.setValue(percent);
                                }
                            };
                            ftp.setCopyStreamListener(streamListener);
                            try (FileOutputStream fos = new FileOutputStream(targetFile.getName())) {
                                ftp.retrieveFile(basePath + "/" + targetFile.getName(), fos);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            d.progressBar.setString("restarting " + targetFile.getName());
                            restartApplication(targetFile.getName());
                        }
                    }

                }
                ftp.logout();
                ftp.disconnect();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    };
    d.thread = longRunningThread;
    d.setVisible(true);
}

From source file:fr.acxio.tools.agia.ftp.FTPDownloadTasklet.java

@Override
public RepeatStatus execute(StepContribution sContribution, ChunkContext sChunkContext) throws Exception {
    FTPClient aClient = ftpClientFactory.getFtpClient();

    RegexFilenameFilter aFilter = new RegexFilenameFilter();
    aFilter.setRegex(regexFilename);//  w  w w .  j  a v a  2 s  . c  o m
    try {
        URI aRemoteBaseURI = new URI(remoteBaseDir);
        URI aRemoteBasePath = new URI(aRemoteBaseURI.toASCIIString() + SEPARATOR);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Listing : [{}] {} ({})", aClient.getRemoteAddress().toString(),
                    aRemoteBaseURI.toASCIIString(), regexFilename);
        }

        FTPFile[] aRemoteFiles = aClient.listFiles(aRemoteBaseURI.toASCIIString(), aFilter);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("  {} file(s) found", aRemoteFiles.length);
        }

        for (FTPFile aRemoteFile : aRemoteFiles) {

            if (sContribution != null) {
                sContribution.incrementReadCount();
            }

            File aLocalFile = new File(localBaseDir, aRemoteFile.getName());
            URI aRemoteTFile = aRemoteBasePath.resolve(aRemoteFile.getName());

            FileOutputStream aOutputStream = new FileOutputStream(aLocalFile);
            try {

                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info(" Downloading : {} => {}", aRemoteTFile.toASCIIString(),
                            aLocalFile.getAbsolutePath());
                }

                aClient.retrieveFile(aRemoteTFile.toASCIIString(), aOutputStream);
                if (removeRemote) {

                    if (LOGGER.isInfoEnabled()) {
                        LOGGER.info(" Deleting : {}", aRemoteTFile.toASCIIString());
                    }

                    aClient.deleteFile(aRemoteTFile.toASCIIString());
                }

                if (sContribution != null) {
                    sContribution.incrementWriteCount(1);
                }

            } finally {
                aOutputStream.close();
            }
        }
    } finally {
        aClient.logout();
        aClient.disconnect();
    }

    return RepeatStatus.FINISHED;
}

From source file:no.imr.sea2data.stox.InstallerUtil.java

public static Boolean retrieveFromFTP(String ftpPath, String outFile, FTPFileFilter filter) {
    try {//from w ww. java  2s .  co  m
        FTPClient ftpClient = new FTPClient();
        // pass directory path on server to connect
        if (ftpPath == null) {
            return false;
        }
        ftpPath = ftpPath.replace("ftp://", "");
        String[] s = ftpPath.split("/", 2);
        if (s.length != 2) {
            return false;
        }
        String server = s[0];
        String subPath = s[1];
        ftpClient.setConnectTimeout(5000);
        ftpClient.connect(server);
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        if (!ftpClient.login("anonymous", "")) {
            return false;
        }
        ftpClient.enterLocalPassiveMode(); // bug : mac doesnt allow ftp server connect through through local firewall - thus use passive server
        try (FileOutputStream fos = new FileOutputStream(outFile)) {
            try {
                String path = subPath + "/";
                FTPFile[] files = ftpClient.listFiles(path, filter);
                Optional<FTPFile> opt = Arrays.stream(files)
                        .sorted((f1, f2) -> Integer.compare(f1.getName().length(), f2.getName().length()))
                        .findFirst();
                if (opt.isPresent()) {
                    ftpClient.retrieveFile(path + opt.get().getName(), fos);
                }
            } finally {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        }
        return true;
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}

From source file:org.amanzi.neo.geoptima.loader.core.parser.impl.FtpDataParser.java

/**
 * @param ftpClient/*from   w ww. ja v  a2 s.c  o  m*/
 * @param file
 * @return
 * @throws IOException
 */
private FTPFile[] getFiles(final FTPClient ftpClient, final FTPFile file) throws IOException {
    FTPFile[] files = ftpClient.listFiles(file.getLink(), FTP_FILE_FILTER);
    for (FTPFile innerFile : files) {
        innerFile.setLink(file.getLink() + "/" + innerFile.getName());
    }
    return files;
}

From source file:org.spka.cursus.publish.website.ftp.ListFiles.java

public void from(FTPClient ftp) throws IOException {
    for (FTPFile file : ftp.listFiles(Constants.RESULTS_DIR, FILES_ONLY)) {
        files.put(Constants.RESULTS_DIR + "/" + file.getName(), null);
    }//w  ww .  j  av  a 2s  .c om
}

From source file:tufts.oki.remoteFiling.RemoteByteStore.java

/**
 *  Get the current length of thsi byte store.
 *
 *  @author Mark Norton/*  ww w . jav  a 2s  .co m*/
 *
 *  @return The current length of this byte store.
 */
public long length() throws osid.filing.FilingException {
    long length = 0;
    try {
        FTPClient client = rc.getClient();
        client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
        //  The file to open consists of the root base plus, path to current directory plus name.
        //String fn = rc.getRootBase() + ((RemoteCabinet)getParent()).separator() + getDisplayName();
        //String fn = rc.getRootBase() + "/" + getDisplayName();
        String fn = getFullName();
        //System.out.println("length - file name to open: " + fn);
        FTPFile[] replies = client.listFiles(__fileListParser, fn);
        System.out.println("File Name = " + fn + " replies =" + replies + "Client:" + client.getStatus());
        if (replies == null) {
            System.out.println(client.getReplyCode());
            throw new osid.filing.FilingException(
                    "RemoteByteStore.length: " + osid.filing.FilingException.IO_ERROR);
        }
        //System.out.println(client.getReplyCode());
        length = replies[0].getSize();
    } catch (IOException e) {
        throw new osid.filing.FilingException(
                "RemoteByteStore.length: " + osid.filing.FilingException.IO_ERROR);
    }
    return length;
}