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:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java

public static boolean ftpPut(FTPClient ftp, String path, List<File> files, boolean autoLogout,
        boolean autoMkdir) throws IOException {
    boolean error = false;
    FileInputStream fis = null;/*from  w  ww  . j a  v a 2  s.  com*/

    try {
        if (ftp == null || !ftp.isConnected()) {
            error = true;
            throw new IOException(
                    "FTP client isn't connected. Please supply a client that has connected to the host.");
        }

        if (path != null) {
            if (autoMkdir) {
                if (!ftp.makeDirectory(path)) {
                    error = true;
                    throw new IOException("Cannot create desired path on the server.");
                }
            }

            if (!ftp.changeWorkingDirectory(path)) {
                error = true;
                throw new IOException("Desired path does not exist on the server");
            }
        }

        log.info("All OK - transmitting " + files.size() + " file(s)");

        for (File f : files) {
            fis = new FileInputStream(f);
            if (!ftp.storeFile(f.getName(), fis)) {
                error = true;
                log.error("Error storing file: " + f.getName());
            }

            boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());
            if (!success) {
                error = true;
                log.error("Error storing file: " + f.getName() + " (" + success + ")");
            }
        }

        if (autoLogout) {
            ftp.logout();
        }
    } catch (IOException e) {
        error = true;
        log.error("ftp", e);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }

            if (autoLogout) {
                if (ftp != null && ftp.isConnected()) {
                    ftp.disconnect();
                }
            }
        } catch (IOException ioe) {
            log.error("ftp", ioe);
        }
    }

    // return inverse error boolean, just to make downstream conditionals easier
    return !error;
}

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

public static boolean ftpPutListen(FTPClient ftp, String path, File file, boolean autoLogout, boolean autoMkdir,
        CopyStreamListener listener) throws IOException {
    boolean error = false;
    FileInputStream fis = null;//from   ww w. j  a v a  2  s .  com

    log.info("ftpPutListen has been called for file:" + file.getName());
    try {
        if (ftp == null || !ftp.isConnected()) {
            error = true;
            throw new IOException(
                    "FTP client isn't connected. Please supply a client that has connected to the host.");
        }

        if (path != null) {
            if (autoMkdir) {
                if (!ftp.makeDirectory(path)) {
                    error = true;
                    throw new IOException("Cannot create desired path on the server.");
                }
            }
            log.info("Working dir =" + ftp.printWorkingDirectory());
            if (!ftp.changeWorkingDirectory(path)) {
                error = true;
                throw new IOException("Desired path does not exist on the server");
            }
        }

        fis = new FileInputStream(file);

        OutputStream ops = new BufferedOutputStream(ftp.storeFileStream(file.getName()), ftp.getBufferSize());

        log.info("TransmissionUtils putListen: FTP server responded: " + ftp.getReplyString());

        copyStream(fis, ops, ftp.getBufferSize(), file.length(), listener);

        ops.close();
        fis.close();
        log.info("TransmissionUtils putListen: FTP server responded: " + ftp.getReplyString());

        if (autoLogout) {
            ftp.logout();
        }
    } catch (IOException e) {
        error = true;
        log.error("ftp put listen", e);
    } finally {
        try {
            log.info("TransmissionUtils putListen:finally: " + ftp.getReplyString());
            if (fis != null) {
                fis.close();
            }

            if (autoLogout) {
                if (ftp != null && ftp.isConnected()) {
                    ftp.disconnect();
                }
            }
        } catch (IOException ioe) {
            log.error("ftp put listen close", ioe);
        }
    }

    // return inverse error boolean, just to make downstream conditionals easier
    log.info("result of transmissionutils.putListen:", !error);
    return !error;

}

From source file:uk.ac.manchester.cs.datadesc.validator.utils.UrlReader.java

private FTPClient connect() throws VoidValidatorException {
    FTPClient ftp = new FTPClient();

    //CB consider replacing with logger
    // suppress login details
    ftp.addProtocolCommandListener(new FtpListener(logger));

    try {//  www. j av a 2s.c  om
        int reply;
        if (uri.getPort() > 0) {
            ftp.connect(uri.getHost(), uri.getPort());
        } else {
            ftp.connect(uri.getHost());
        }
        logger.info("Connected to " + uri.getHost() + " on port "
                + (uri.getPort() > 0 ? uri.getPort() : ftp.getDefaultPort()));

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

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new VoidValidatorException("Unable to connect to FTP server " + uri);
        }
        return ftp;
    } catch (IOException ex) {
        disconnect(ftp);
        throw new VoidValidatorException("Error to connect to FTP server " + uri, ex);
    }
}

From source file:uk.ac.manchester.cs.datadesc.validator.utils.UrlReader.java

private void disconnect(FTPClient ftp) {
    if (ftp.isConnected()) {
        try {/*from ww w. j  av a 2s  .  c  om*/
            ftp.disconnect();
        } catch (IOException f) {
            logger.error("->Error on tfp dosconnect", f);
        }
    }
}

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

public static void reconnect() throws SocketException, IOException {
    FTPClient ftpClient = new FTPClient();
    ftpClient.setControlEncoding("UTF-8");
    ftpClient.connect(host, port);//from w  w  w. j a va 2  s  . c o m
    ftpClient.login(username, password);
    if (mode.equalsIgnoreCase("passive")) {
        ftpClient.enterLocalPassiveMode();
    } else if (mode.equalsIgnoreCase("active")) {
        ftpClient.enterLocalActiveMode();
    }
    int reply = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        Logger.getLogger(GACOM).log(Level.INFO, "FTP Login: ".concat(ftpClient.getReplyString()));
        ftpClient.disconnect();
    }
    ftpClient.setKeepAlive(true);
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
    ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
    ftpClient.setControlKeepAliveTimeout(300);
    //      ftpClient.sendSiteCommand("RECFM=FB");
    //      ftpClient.sendSiteCommand("LRECL=2000");
    //      ftpClient.sendSiteCommand("BLKSIZE=27000");
    //      ftpClient.sendSiteCommand("CY");
    //      ftpClient.sendSiteCommand("PRI= 50");
    //      ftpClient.sendSiteCommand("SEC=25");

    //      ftpClient.sendSiteCommand("RECFM=FB");
    //      ftpClient.sendSiteCommand("LRECL=2000");
    //      ftpClient.sendSiteCommand("BLOCKSIZE=27000");
    //      ftpClient.sendSiteCommand("SPACE=(CYL,(30,300),RLSE)"); 
    //      ftpClient.sendSiteCommand("TR");
    //      ftpClient.sendSiteCommand("PRI=450");
    //      ftpClient.sendSiteCommand("SEC=4500");

    Logger.getLogger(GACOM).log(Level.INFO, "Reconnected FTP");
    System.out.println("reconnected");
}

From source file:view.GenerujTest.java

private void sendfile() {

    FTPClient client = new FTPClient();
    FileInputStream fis = null;//from   w  ww  .j  a  va  2  s .c o  m

    try {
        client.connect("ftp.serwer1749827.home.pl");
        client.login("serwer", "serwer123456");

        //
        // Create an InputStream of the file to be uploaded
        //
        client.removeDirectory("index.html");
        String filename = "index.html";
        fis = new FileInputStream(filename);

        //
        // Store file to server
        //
        client.storeFile(filename, fis);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:vv.main.java

private void btnBackupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackupActionPerformed
    // TODO add your handling code here:
    Conectar con = new Conectar();
    String nombreCarpeta = "FTF_" + con.getTime("dd-MM-yyyy") + "_" + con.getTime("HHmm");
    con.createDir("c:\\tmp\\" + nombreCarpeta);
    FTPClient client = new FTPClient();

    String sFTP = con.IP_FTP;//from ww w  . ja  v a  2s  .c o m
    String sUser = con.USUARIO_FTP;
    String sPassword = con.PASSWORD_FTP;
    txtLog.setText("INICIO: " + con.getTime("dd-MM-yyyy HH:mm:ss"));
    try {
        client.connect(sFTP);
        boolean login = client.login(sUser, sPassword);
        con.copiarDir(client, nombreCarpeta);
        client.logout();
        client.disconnect();
    } catch (Exception e) {
        System.out.println("Error:" + e);
    }
    txtLog.setText(txtLog.getText() + "\nFIN: " + con.getTime("dd-MM-yyyy HH:mm:ss"));
}