Example usage for org.apache.commons.net.ftp FTPClient isAvailable

List of usage examples for org.apache.commons.net.ftp FTPClient isAvailable

Introduction

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

Prototype

public boolean isAvailable() 

Source Link

Document

Make various checks on the socket to test if it is available for use.

Usage

From source file:net.audumla.climate.bom.BOMDataLoader.java

private synchronized FTPClient getFTPClient(String host) {
    FTPClient ftp = ftpClients.get(host);
    if (ftp == null || !ftp.isAvailable() || !ftp.isConnected()) {
        ftp = new FTPClient();
        FTPClientConfig config = new FTPClientConfig();
        ftp.configure(config);//from  w  ww . ja  v  a  2s . com
        try {
            ftp.setControlKeepAliveTimeout(30);
            ftp.setControlKeepAliveReplyTimeout(5);
            ftp.setDataTimeout(3000);
            ftp.setDefaultTimeout(1000);
            int reply;
            ftp.connect(host);
            LOG.debug("Connected to " + host);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                LOG.error("FTP server '" + host + "' refused connection.");
            } else {
                if (!ftp.login("anonymous", "guest")) {
                    LOG.error("Unable to login to server " + host);
                }
                ftp.setSoTimeout(60000);
                ftp.enterLocalPassiveMode();
                ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

            }
        } catch (IOException e) {
            LOG.error("Unable to connect to " + host, e);
        }
        ftpClients.put(host, ftp);
    }
    if (!ftp.isConnected() || !ftp.isAvailable()) {
        throw new UnsupportedOperationException("Cannot connect to " + host);
    }
    return ftp;
}

From source file:s32a.FTPTest.java

public FTPTest() {
    FTPClient client = new FTPSClient(false);
    FileInputStream fis = null;//from  ww  w .j a v  a 2s  .  c  om
    FileOutputStream fos = null;

    try {
        System.out.println("connecting");
        client.connect("athena.fhict.nl");
        boolean login = client.login("i293443", "ifvr2edfh101");
        System.out.println("login: " + login);
        client.enterLocalPassiveMode();
        System.out.println("connected: " + client.isConnected() + ", available: " + client.isAvailable());

        client.setFileType(FTP.ASCII_FILE_TYPE);
        //
        // Create an InputStream of the file to be uploaded
        //
        String filename = ".gitattributes";
        File file = new File(filename);
        file.createNewFile();
        System.out.println(file.length());
        fis = new FileInputStream(file.getAbsolutePath());
        client.makeDirectory("/Airhockey/Codebase/test");
        client.makeDirectory("\\Airhockey\\Codebase\\testey");

        //
        // Store file to server
        //
        String desiredName = "s32a\\Server\\.gitattributes";
        System.out.println("storefile: " + file.getAbsolutePath() + " - "
                + client.storeFile("/Airhockey/" + desiredName, fis));
        System.out.println("file stored");

        //            File output = new File("colors.json");
        //            fos = new FileOutputStream(output.getAbsolutePath());
        //            client.retrieveFile("/colors.json", fos);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception ex) {
        System.out.println("exception caught: " + ex.getMessage());
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:uk.sipperfly.utils.FTPUtil.java

/**
 * Upload whole directory (including its nested sub directories and files) to FTP server.
 *
 * @param ftpClient       an instance of org.apache.commons.net.ftp.FTPClient class.
 * @param remoteDirPath   Path of the destination directory on the server.
 * @param localParentDir  Path of the local directory being uploaded.
 * @param remoteParentDir Path of the parent directory of the current directory on the server (used by recursive calls).
 * @throws IOException if any network or IO error occurred.
 *///  www .  ja  v a  2  s.co  m
public static boolean uploadDirectory(FTPClient ftpClient, String remoteDirPath, String localParentDir,
        String remoteParentDir) throws IOException {

    System.out.println("LISTING directory: " + localParentDir);
    Logger.getLogger(GACOM).log(Level.INFO, "LISTING directory: {0}".concat(localParentDir));
    File localDir = new File(localParentDir);
    File[] subFiles = localDir.listFiles();
    if (subFiles != null && subFiles.length > 0) {
        for (File item : subFiles) {
            boolean answer = ftpClient.sendNoOp();
            if (!answer) {
                reconnect();
            }
            String status = ftpClient.getStatus();
            boolean a = ftpClient.isAvailable();
            //            if (!ftpClient.isConnected()) {
            //               reconnect();
            //            }
            String remoteFilePath = remoteDirPath + "/" + remoteParentDir + "/" + item.getName();
            if (remoteParentDir.equals("")) {
                remoteFilePath = remoteDirPath + "/" + item.getName();
            }
            if (item.isFile()) {
                // upload the file
                String localFilePath = item.getAbsolutePath();
                Logger.getLogger(GACOM).log(Level.INFO, "About to upload the file: ".concat(localFilePath));
                System.out.println("About to upload the file: " + localFilePath);
                boolean uploaded = uploadSingleFile(ftpClient, localFilePath, remoteFilePath);

                if (uploaded) {
                    Logger.getLogger(GACOM).log(Level.INFO, "UPLOADED a file to: ".concat(remoteFilePath));
                    System.out.println("UPLOADED a file to: " + remoteFilePath);
                } else {
                    System.out.println("COULD NOT upload the file: " + localFilePath);
                    Logger.getLogger(GACOM).log(Level.INFO,
                            "COULD NOT upload the file: ".concat(localFilePath));
                    Logger.getLogger(GACOM).log(Level.INFO, ftpClient.getReplyString());
                    return false;
                }
            } else {
                // create directory on the server
                boolean created = ftpClient.makeDirectory(remoteFilePath);
                if (created) {
                    System.out.println("CREATED the directory: " + remoteFilePath);
                    Logger.getLogger(GACOM).log(Level.INFO, "CREATED the directory: ".concat(remoteFilePath));
                } else {
                    System.out.println("COULD NOT create the directory: " + remoteFilePath);
                    Logger.getLogger(GACOM).log(Level.INFO,
                            "COULD NOT create the directory: ".concat(remoteFilePath));
                    Logger.getLogger(GACOM).log(Level.INFO, ftpClient.getReplyString());
                    return false;
                }

                // upload the sub directory
                String parent = remoteParentDir + "/" + item.getName();
                if (remoteParentDir.equals("")) {
                    parent = item.getName();
                }

                localParentDir = item.getAbsolutePath();
                uploadDirectory(ftpClient, remoteDirPath, localParentDir, parent);
            }
        }
    }
    return true;
}