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:org.programmatori.domotica.own.plugin.remote.FTPUtility.java

/**
 * Funzione che consente la connessione ad un Server FTP
 * //w  w  w  .j a va2 s  .  c om
 * @param ftpServer Server FTP
 * @param username Nome utente per l'accesso
 * @param password Password per l'accesso
 * @return Un oggetto di tipo FTPClient contenente il Client per l'accesso
 */
public static FTPClient connect(String ftpServer, String username, String password) {

    FTPClient ftp = new FTPClient();
    String replyString;
    try {
        ftp.connect(ftpServer);
        ftp.login(username, password);
        log.info("Connesso a " + ftpServer + ".");

        replyString = ftp.getReplyString();
        log.debug(replyString);

        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            log.error("Il Server FTP ha rifiutato la connessione.");
            log.error(replyString);
            return null;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return ftp;
}

From source file:org.programmatori.domotica.own.plugin.remote.FTPUtility.java

/**
 * Funzione che consente la disconnessione da un Server FTP
 * //  www  .  j a va 2 s.com
 * @param ftp Offetto di tipo FTPClient utilizzato per l'accesso
 * @return >0 se tutto funziona correttamente; <0 in caso di errori
 */
public static int disconnect(FTPClient ftp) {
    if (ftp.isConnected()) {
        try {
            ftp.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }
    log.info("Disconnessione avvenuta con successo.");
    return 1;
}

From source file:org.ramadda.repository.monitor.FtpAction.java

/**
 * _more_/*from  w  w w  . j ava2 s .c o  m*/
 *
 *
 * @param monitor _more_
 * @param entry _more_
 */
protected void entryMatched(EntryMonitor monitor, Entry entry) {
    FTPClient ftpClient = new FTPClient();
    try {
        Resource resource = entry.getResource();
        if (!resource.isFile()) {
            return;
        }
        if (server.length() == 0) {
            return;
        }

        String passwordToUse = monitor.getRepository().getPageHandler().processTemplate(password, false);
        ftpClient.connect(server);
        if (user.length() > 0) {
            ftpClient.login(user, password);
        }
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            monitor.handleError("FTP server refused connection:" + server, null);

            return;
        }
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        if (directory.length() > 0) {
            ftpClient.changeWorkingDirectory(directory);
        }

        String filename = monitor.getRepository().getEntryManager().replaceMacros(entry, fileTemplate);

        InputStream is = new BufferedInputStream(
                monitor.getRepository().getStorageManager().getFileInputStream(new File(resource.getPath())));
        boolean ok = ftpClient.storeUniqueFile(filename, is);
        is.close();
        if (ok) {
            monitor.logInfo("Wrote file:" + directory + " " + filename);
        } else {
            monitor.handleError("Failed to write file:" + directory + " " + filename, null);
        }
    } catch (Exception exc) {
        monitor.handleError("Error posting to FTP:" + server, exc);
    } finally {
        try {
            ftpClient.logout();
        } catch (Exception exc) {
        }
        try {
            ftpClient.disconnect();
        } catch (Exception exc) {
        }
    }
}

From source file:org.ramadda.repository.type.FtpTypeHandler.java

/**
 * _more_/*from   w ww .j a  va 2 s . c  om*/
 *
 * @param ftpClient _more_
 */
private static void closeConnection(FTPClient ftpClient) {
    try {
        ftpClient.logout();
    } catch (Exception exc) {
    }
    try {
        ftpClient.disconnect();
    } catch (Exception exc) {
    }
}

From source file:org.ramadda.repository.type.FtpTypeHandler.java

/**
 * _more_/*w w w. j a  v a2 s .c om*/
 *
 * @param server _more_
 * @param baseDir _more_
 * @param user _more_
 * @param password _more_
 *
 * @return _more_
 *
 * @throws Exception _more_
 */
public static String test(String server, String baseDir, String user, String password) throws Exception {
    FTPClient ftpClient = new FTPClient();
    try {
        String file = baseDir;
        ftpClient.connect(server);
        //System.out.print(ftp.getReplyString());
        ftpClient.login(user, password);
        //            System.out.print(ftpClient.getReplyString());
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            System.err.println("FTP server refused connection.");

            return null;
        }
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();

        boolean isDir = isDir(ftpClient, file);
        //            System.err.println("file:" + file + " is dir: " + isDir);

        if (isDir) {
            FTPFile[] files = ftpClient.listFiles(file);
            for (int i = 0; i < files.length; i++) {
                //                    System.err.println ("f:" + files[i].getName() + " " + files[i].isDirectory() + "  " + files[i].isFile());
            }
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            if (ftpClient.retrieveFile(file, bos)) {
                //                    System.err.println(new String(bos.toByteArray()));
            } else {
                throw new IOException("Unable to retrieve file:" + file);
            }
        }

        return "";
    } finally {
        closeConnection(ftpClient);
    }
}

From source file:org.ramadda.repository.type.FtpTypeHandler.java

/**
 * _more_/*from  ww w  . j a v  a  2s.c  om*/
 *
 * @param parentEntry _more_
 *
 * @return _more_
 *
 * @throws Exception _more_
 */
private FTPClient getFtpClient(Entry parentEntry) throws Exception {
    Object[] values = parentEntry.getValues();
    if (values == null) {
        return null;
    }
    String server = (String) values[COL_SERVER];
    String baseDir = (String) values[COL_BASEDIR];
    String user = (String) values[COL_USER];
    String password = (String) values[COL_PASSWORD];
    if (password != null) {
        password = getRepository().getPageHandler().processTemplate(password, false);
    } else {
        password = "";
    }
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(server);
        if (user != null) {
            ftpClient.login(user, password);
        }
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            System.err.println("FTP server refused connection.");

            return null;
        }
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();

        return ftpClient;
    } catch (Exception exc) {
        System.err.println("Could not connect to ftp server:" + server + "\nError:" + exc);

        return null;
    }
}

From source file:org.ramadda.util.Utils.java

/**
 * _more_/*w w  w . j  a v a2s.c  o  m*/
 *
 * @param url _more_
 *
 * @return _more_
 *
 * @throws Exception _more_
 */
public static FTPClient makeFTPClient(URL url) throws Exception {
    FTPClient ftpClient = new FTPClient();
    ftpClient.connect(url.getHost());
    ftpClient.login("anonymous", "");
    int reply = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftpClient.disconnect();
        System.err.println("FTP server refused connection.");

        return null;
    }
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClient.enterLocalPassiveMode();

    return ftpClient;
}

From source file:org.ramadda.util.Utils.java

/**
 * _more_/*ww w .j  a  va 2 s . c  o m*/
 *
 * @param ftpClient _more_
 */
public static void closeConnection(FTPClient ftpClient) {
    try {
        ftpClient.logout();
    } catch (Exception exc) {
    }
    try {
        ftpClient.disconnect();
    } catch (Exception exc) {
    }
}

From source file:org.shept.util.FtpFileCopy.java

public int ftpSyncLocal(FtpConfig config, String localPath, String filePattern) {
    boolean rc = false;
    FTPClient ftpC = new FTPClient();
    int num = -1;
    try {//ww w .j a v a2  s  .c  o m
        rc = configSetup(config, ftpC);
        if (!rc) {
            return 0;
        }
        num = syncPull(ftpC, localPath, filePattern);
    } catch (Exception ex) {
        logger.error("Ftp preparing fileCopy did not succeed: " + config.toString(), ex);
    }
    try {
        ftpC.disconnect();
    } catch (Exception ex) {
    }
    ;
    return num;
}

From source file:org.shept.util.FtpFileCopy.java

public int ftpSyncRemote(FtpConfig config, String remotePath, String filePattern) {
    boolean rc = false;
    FTPClient ftpC = new FTPClient();
    int num = -1;
    try {/*  w w  w .jav a2  s. c  om*/
        rc = configSetup(config, ftpC);
        if (!rc) {
            return 0;
        }
        num = syncPush(remotePath, ftpC, filePattern);
    } catch (Exception ex) {
        logger.error("Ftp preparing fileCopy did not succeed: " + config.toString(), ex);
    }
    try {
        ftpC.disconnect();
    } catch (Exception ex) {
    }
    ;
    return num;
}