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

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

Introduction

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

Prototype

public boolean isFile() 

Source Link

Document

Determine if the file is a regular file.

Usage

From source file:de.l3s.dlg.ncbikraken.ftp.NcbiFTPClient.java

public static void getMedline(MedlineFileType type) {
    FileOutputStream out = null;//from   ww w.ja va  2s.c  o m
    FTPClient ftp = new FTPClient();
    try {
        // Connection String
        LOGGER.info("Connecting to FTP server " + SERVER_NAME);
        ftp.connect(SERVER_NAME);
        ftp.login("anonymous", "");
        ftp.cwd(BASELINE_PATH);
        ftp.cwd(type.getServerPath());

        try {
            ftp.pasv();
        } catch (IOException e) {
            LOGGER.error(
                    "Can not access the passive mode. Maybe a problem with your (Windows) firewall. Just try to run as administrator: \nnetsh advfirewall set global StatefulFTP disable");
            return;
        }

        for (FTPFile file : ftp.listFiles()) {
            if (file.isFile()) {
                File meshF = new File(file.getName());
                LOGGER.debug("Downloading file: " + SERVER_NAME + ":" + BASELINE_PATH + "/"
                        + type.getServerPath() + "/" + meshF.getName());
                out = new FileOutputStream(Configuration.INSTANCE.getMedlineDir() + File.separator + meshF);
                ftp.retrieveFile(meshF.getName(), out);
                out.flush();
                out.close();
            }
        }

    } catch (IOException ioe) {
        LOGGER.error(ioe.getMessage());

    } finally {
        IOUtils.closeQuietly(out);
        try {
            ftp.disconnect();
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
        }
    }

}

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

/**
 * ??Map/*from  w w w  .  j  a  va  2  s .  co 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:main.TestManager.java

/**
 * Deletes all files on the server and uploads all the
 * tests from the local directory./*  ww  w  .java 2  s . com*/
 */
public static void syncServerWithTest() {
    FTPClient ftp = new FTPClient();
    boolean error = false;
    try {
        int reply;
        String server = "zajicek.endora.cz";
        ftp.connect(server, 21);

        // After connection attempt, we check the reply code to verify
        // success.
        reply = ftp.getReplyCode();

        //Not connected successfully - inform the user
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            Debugger.println("FTP server refused connection.");
            ErrorInformer.failedSyncing();
            return;
        }
        // LOGIN
        boolean success = ftp.login("plakato", "L13nK4");
        Debugger.println("Login successful: " + success);
        if (success == false) {
            ftp.disconnect();
            ErrorInformer.failedSyncing();
            return;
        }
        ftp.enterLocalPassiveMode();
        // Get all files from the server
        FTPFile[] files = ftp.listFiles();
        System.out.println("Got files! Count: " + files.length);
        // Delete all current tests on the server to be replaced with 
        // actualized version from local directory
        for (FTPFile f : files) {
            if (f.getName() == "." || f.getName() == "..") {
                continue;
            }
            if (f.isFile()) {
                ftp.deleteFile(f.getName());
            }
        }
        // Copy the files from local folder to server
        File localFolder = new File("src/tests/");
        File[] localFiles = localFolder.listFiles();
        int failed = 0;
        for (File f : localFiles) {
            if (f.getName() == "." || f.getName() == "..") {
                continue;
            }
            if (f.isFile()) {
                Debugger.println(f.getName());
                File file = new File("src/tests/" + f.getName());
                InputStream input = new FileInputStream(file);
                if (!ftp.storeFile(f.getName(), input))
                    failed++;
                input.close();
            }
        }
        // If we failed to upload some file, inform the user
        if (failed != 0) {
            ftp.disconnect();
            ErrorInformer.failedSyncing();
            return;
        }
        // Disconnect ftp client or resolve the potential error
        ftp.logout();
    } catch (IOException e) {
        error = true;
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
                // do nothing
            }
        }
        if (error) {
            ErrorInformer.failedSyncing();
            return;
        }
    }
    Alert alert = new Alert(AlertType.INFORMATION);
    alert.setTitle("Upload hotov");
    alert.setHeaderText(null);
    alert.setContentText("spene sa podarilo skoprova vetky testy na server!");

    alert.showAndWait();
    alert.close();
}

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;
        }/*w  w w.  j  av  a 2s . c o m*/
    }
    return false;
}

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 .j a  va 2 s .  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:ftpclientgui.FTPListItemAdapter.java

@Override
public FileListItem getFileListItem(FTPFile adaptee) {
    FileListItemTypes t = null;//from   w  w w .  java  2  s .c  om
    if (adaptee.isDirectory()) {
        t = FileListItemTypes.DIRECTORY;
    } else if (adaptee.isFile()) {
        t = FileListItemTypes.FILE;
    }
    return new FileListItem(adaptee.getName(), t);
}

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

/**
 * Recursively retrieve a directory listing from the FTP server.
 * /*ww w  .  j  a v  a2 s .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.claim.support.FtpUtil.java

public void uploadMutiFileWithFTP(String targetDirectory, FileTransferController fileTransferController) {
    try {/*from   w  w w  . java2s.  co m*/
        FtpProperties properties = new ResourcesProperties().loadFTPProperties();

        FTPClient ftp = new FTPClient();
        ftp.connect(properties.getFtp_server());
        if (!ftp.login(properties.getFtp_username(), properties.getFtp_password())) {
            ftp.logout();
        }
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }
        ftp.enterLocalPassiveMode();
        System.out.println("Remote system is " + ftp.getSystemName());
        ftp.changeWorkingDirectory(targetDirectory);
        System.out.println("Current directory is " + ftp.printWorkingDirectory());
        FTPFile[] ftpFiles = ftp.listFiles();
        if (ftpFiles != null && ftpFiles.length > 0) {
            for (FTPFile file : ftpFiles) {
                if (!file.isFile()) {
                    continue;
                }
                System.out.println("File is " + file.getName());
                OutputStream output;
                output = new FileOutputStream(
                        properties.getFtp_remote_directory() + File.separator + file.getName());
                ftp.retrieveFile(file.getName(), output);
                output.close();
            }
        }
        ftp.logout();
        ftp.disconnect();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:ftpclient.FTPManager.java

public FTPFile[] getCurrentDirectoryFilesList() throws IOException {
    if (client.isConnected()) {
        FTPFile[] fs = client.listFiles();
        //listFiles returns both files and directories!
        List<FTPFile> fsList = new ArrayList<>();
        for (FTPFile f : fs) {
            if (f.isFile()) {
                fsList.add(f);/*from ww  w .ja v a 2s . com*/
            }
        }
        Collections.sort(fsList, new Comparator<FTPFile>() {

            @Override
            public int compare(FTPFile o1, FTPFile o2) {
                return o1.getName().compareToIgnoreCase(o2.getName());
            }

        });

        return (FTPFile[]) fsList.toArray(new FTPFile[] {});
    } else {
        return null;
    }
}

From source file:com.esri.gpt.control.webharvest.client.waf.FtpFileIterator.java

/**
 * Creates new resource.// ww  w.  j  a  v a 2 s . c o m
 * @param file file
 * @return resource
 */
protected Resource newResource(FTPFile file) {
    if (file.isDirectory()) {
        return new FtpFolder(iterationContext, getFtpClient(), criteria, folder + "/" + file.getName());
    } else if (file.isFile() && file.getName().toLowerCase().endsWith(".xml")) {
        if (criteria == null || criteria.getFromDate() == null || file.getTimestamp() == null
                || (criteria.getFromDate() != null && file.getTimestamp() != null
                        && file.getTimestamp().after(criteria.getFromDate()))) {
            return new FtpFile(iterationContext, getFtpClient(), folder + "/" + file.getName());
        }
    }
    return null;
}