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

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

Introduction

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

Prototype

@Override
public void disconnect() throws IOException 

Source Link

Document

Closes the connection to the FTP server and restores connection parameters to the default values.

Usage

From source file:ubic.basecode.util.NetUtils.java

/**
 * Convenient method to get a FTP connection.
 * /* w w w .j a  v a 2s  .c  om*/
 * @param host
 * @param login
 * @param password
 * @param mode
 * @return
 * @throws SocketException
 * @throws IOException
 */
public static FTPClient connect(int mode, String host, String loginName, String password)
        throws SocketException, IOException {
    FTPClient f = new FTPClient();
    f.enterLocalPassiveMode();
    f.setBufferSize(32 * 2 ^ 20);
    boolean success = false;
    f.connect(host);
    int reply = f.getReplyCode();
    if (FTPReply.isPositiveCompletion(reply))
        success = f.login(loginName, password);
    if (!success) {
        f.disconnect();
        throw new IOException("Couldn't connect to " + host);
    }
    f.setFileType(mode);
    log.debug("Connected to " + host);
    return f;
}

From source file:ubic.gemma.core.util.NetDatasourceUtil.java

public void disconnect(FTPClient f) throws IOException {
    f.disconnect();
}

From source file:ubic.gemma.util.NetDatasourceUtil.java

/**
 * @param f
 * @throws IOException
 */
public void disconnect(FTPClient f) throws IOException {
    f.disconnect();
}

From source file:ubicrypt.core.provider.ftp.FTProvider.java

private Observable<FTPClient> connect() {
    return Observable.<FTPClient>create(subscriber -> {
        final FTPClient client = new FTPClient();
        try {/*from   w  ww.j  a v a2 s  .  c om*/
            client.connect(conf.getHost(), getConf().getPort() == -1 ? 21 : getConf().getPort());
            final int reply = client.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                log.error("FTP server refused connection:" + client.getReplyString());
                if (client.isConnected()) {
                    client.disconnect();
                }
                subscriber.onError(
                        new RuntimeException("FTP server refused connection:" + client.getReplyString()));
                return;
            }
            if (!getConf().isAnonymous()) {
                if (!client.login(getConf().getUsername(), new String(getConf().getPassword()))) {
                    client.disconnect();
                    log.warn("FTP wrong credentials:" + client.getReplyString());
                    subscriber.onError(new RuntimeException("FTP wrong credentials"));
                }
            }
            client.setFileType(FTP.BINARY_FILE_TYPE);
            client.setBufferSize(1 << 64);
            client.enterLocalPassiveMode();
            client.setControlKeepAliveTimeout(60 * 60); //1h
            if (!isEmpty(conf.getFolder())) {
                final String directory = startsWith("/", conf.getFolder()) ? conf.getFolder()
                        : "/" + conf.getFolder();
                if (!client.changeWorkingDirectory(directory)) {
                    if (!client.makeDirectory(directory)) {
                        disconnect(client);
                        subscriber.onError(new ProviderException(showServerReply(client)));
                        return;
                    }
                    if (!client.changeWorkingDirectory(directory)) {
                        disconnect(client);
                        subscriber.onError(new ProviderException(showServerReply(client)));
                        return;
                    }
                }
            }
            subscriber.onNext(client);
            subscriber.onCompleted();
        } catch (final IOException e) {
            disconnect(client);
            subscriber.onError(e);
        }
    }).subscribeOn(Schedulers.io());
}

From source file:ubicrypt.core.provider.ftp.FTProvider.java

private void disconnect(FTPClient client) {
    try {/*  w w  w.j a  v  a  2  s .  c  om*/
        if (client.isConnected()) {
            client.disconnect();
        }
    } catch (final IOException e1) {

    }
}

From source file:ubicrypt.core.provider.ftp.FTProvider.java

private void close(final FTPClient client) {
    try {//from  www .  j  a  va  2 s.c  om
        client.logout();
    } catch (final IOException e1) {
    }
    try {
        client.disconnect();
    } catch (final IOException e1) {
    }
}

From source file:ucar.unidata.idv.ui.ImageGenerator.java

/**
 * IS the FtpClient in an ok state. If it isn't then disconnect it and throw and IllegalStateException
 *
 * @param f Ftp client/* w w  w. ja va 2 s.c o  m*/
 * @param msg Message to use if in error
 *
 * @throws Exception On badness
 */
private static void checkFtp(FTPClient f, String msg) throws Exception {
    int replyCode = f.getReplyCode();
    if (!FTPReply.isPositiveCompletion(replyCode)) {
        String reply = f.getReplyString();
        f.disconnect();
        throw new IllegalStateException("Error with ftp: " + replyCode + " " + msg + "\n" + reply);
    }
}

From source file:ucar.unidata.idv.ui.ImageGenerator.java

/**
 * Do an FTP put of the given bytes/*from   w ww.j  av a2 s.c  o m*/
 *
 * @param server server
 * @param userName user name on server
 * @param password password on server
 * @param destination Where to put the bytes
 * @param bytes The bytes
 *
 * @throws Exception On badness
 */
public static void ftpPut(String server, String userName, String password, String destination, byte[] bytes)
        throws Exception {
    FTPClient f = new FTPClient();

    f.connect(server);
    f.login(userName, password);
    f.setFileType(FTP.BINARY_FILE_TYPE);
    f.enterLocalPassiveMode();
    checkFtp(f, "Connecting to ftp server");
    f.storeFile(destination, new ByteArrayInputStream(bytes));
    checkFtp(f, "Storing file");
    f.logout();
    f.disconnect();
}

From source file:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java

public static FTPClient ftpConnect(String host, String username, String password) throws IOException {
    FTPClient ftp = new FTPClient();
    try {/* ww w  .  j a  v a2  s. c  o m*/

        ftp.connect(host);
        log.debug("Trying " + host);
        log.debug(ftp.getReplyString());
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new IOException("FTP server refused connection: " + reply);
        } else {
            log.info("Connected");
        }

        ftp.login(username, password);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
    } catch (NoRouteToHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    } catch (UnknownHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    }
    return ftp;
}

From source file:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java

public static FTPClient ftpConnect(String host, int port, String username, String password) throws IOException {
    FTPClient ftp = new FTPClient();
    try {// www . j av a2 s . co  m
        ftp.connect(host, port);
        log.debug("Trying " + host + ":" + port);
        log.debug(ftp.getReplyString());
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new IOException("FTP server refused connection: " + reply);
        } else {
            log.debug("Connected");
        }

        ftp.login(username, password);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
    } catch (NoRouteToHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    } catch (UnknownHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    }
    return ftp;
}