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

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

Introduction

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

Prototype

public void setSoTimeout(int timeout) throws SocketException 

Source Link

Document

Set the timeout in milliseconds of a currently open connection.

Usage

From source file:org.ut.biolab.medsavant.shared.util.SeekableFTPStream.java

private FTPClient getFTPClient() throws IOException {
    if (ftpClient == null) {
        FTPClient client = new FTPClient();
        try {//from  w ww .  jav  a2  s . co  m
            client.connect(host, port);
            int reply = client.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                throw new IOException("Unable to connect to " + host);
            }
            if (!client.login(username, password)) {
                throw new IOException("Unable to login to " + host + " as " + username);
            }
            client.setFileType(FTP.BINARY_FILE_TYPE);
            client.enterLocalPassiveMode();
            client.setSoTimeout(SOCKET_TIMEOUT);
            ftpClient = client;
            client = null;
        } finally {
            if (client != null) {
                client.disconnect();
            }
        }
    }

    return ftpClient;
}

From source file:rapture.ftp.common.FTPConnection.java

@Override
public boolean connectAndLogin() {
    if (isLocal()) {
        log.info("In local mode - not connecting");
        return true;
    }//from  w w w . j a  v  a  2  s . c o  m
    return FTPService.runWithRetry("Could not login to " + config.getAddress() + " as " + config.getLoginId(),
            this, false, new FTPAction<Boolean>() {
                @Override
                public Boolean run(int attemptNum) throws IOException {
                    FTPClient ftpClient = getFtpClient();
                    log.debug(String.format("Connecting to %s. Attempt %s of %s", config.getAddress(),
                            1 + attemptNum, config.getRetryCount()));
                    try {
                        ftpClient.connect(config.getAddress());
                    } catch (UnknownHostException e) {
                        log.info(ExceptionToString.summary(e));
                        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR,
                                "Unknown host " + config.getAddress());
                    }
                    int reply = ftpClient.getReplyCode();
                    if (!FTPReply.isPositiveCompletion(reply)) {
                        log.debug("Got non-positive reply of " + reply);
                        logoffAndDisconnect();
                        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR,
                                "Could not connect to: " + config.getAddress());
                    }
                    log.debug("Logging in user: " + config.getLoginId());
                    if (!ftpClient.login(config.getLoginId(), config.getPassword())) {
                        log.info("Could not login to " + config.getAddress() + " as " + config.getLoginId());
                        ftpClient.logout();
                        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR,
                                "Could not login to " + config.getAddress() + " as " + config.getLoginId());
                    }
                    isLoggedIn = true;
                    log.debug("Entering local passive mode");
                    ftpClient.enterLocalPassiveMode();
                    log.info("Connected and logged in to: " + config.getAddress());
                    ftpClient.setSoTimeout(1000 * config.getTimeout());
                    return true;
                }
            });
}