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

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

Introduction

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

Prototype

public boolean logout() throws IOException 

Source Link

Document

Logout of the FTP server by sending the QUIT command.

Usage

From source file:CFDI_Verification.CFDI_Verification.java

public static String test_Connection(String directory) {

    String server = "192.1.1.64";
    String user = "ftpuser";
    String pass = "Oracle123";
    String result = "No Connected!";

    FTPClient ftpClient = new FTPClient();

    try {//from  w  ww.  jav  a 2  s  . c o m
        ftpClient.connect(server);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.changeWorkingDirectory(directory);

        if (ftpClient.isConnected() == true) {
            result = "Connected to " + ftpClient.printWorkingDirectory();
        }

        ftpClient.logout();
        ftpClient.disconnect();

    } catch (IOException ex) {
        System.out.println("Ooops! Error en conexin." + ex.getMessage());
    } finally {
        return result;
    }
}

From source file:com.glaf.core.util.FtpUtils.java

/**
 * FTP/*from  w  w  w .j  av  a2s  . com*/
 */
public static void closeConnect(FTPClient ftpClient) {
    try {
        ftpClient.logout();
        ftpClient.disconnect();
        logger.info("disconnect success");
    } catch (IOException ex) {
        logger.error("disconnect error", ex);
        throw new RuntimeException(ex);
    }
}

From source file:AIR.Common.Web.FileFtpHandler.java

/**
 * Makes FTP call to the provided URI and retrieves contents
 *  /*from   w ww  . j  a v a2  s. c o m*/
 * @param uri
 * @return ByteArrayInputStream
 * @throws FtpResourceException
 */
public static byte[] getBytes(URI uri) throws FtpResourceException {
    try {
        FTPClient ftp = new FTPClient();
        String[] credentialsAndHost = uri.getAuthority().split("@");
        String host = credentialsAndHost[1];
        String[] credentials = credentialsAndHost[0].split(":");

        ftp.connect(host);
        ftp.enterLocalPassiveMode();
        if (!ftp.login(credentials[0], credentials[1])) {
            ftp.logout();
            ftp.disconnect();
            throw new RuntimeException("FTP Authentication Failure");
        }
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.logout();
            ftp.disconnect();
            throw new RuntimeException("FTP No reponse from server");
        }
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ftp.retrieveFile(uri.getPath(), output);
        output.close();
        ftp.logout();
        ftp.disconnect();
        return output.toByteArray();

    } catch (IOException ex) {
        throw new FtpResourceException(ex);
    }
}

From source file:ftp.FTPtask.java

/**
 * Upload a file to a FTP server/*  w w w  .  jav a  2 s.co m*/
  * @param FTPADDR, ?  ?
  * @param user,   
  * @param password,   
 * @param PathOnFtp,      -  /upload/touch.dat
 * @param FilenameOnLocalMachine,       -  C:/somefile.txt
 */

public static void UploadFileOnFtp(String FTPADDR, String user, String password, String PathOnFtp,
        String FilenameOnLocalMachine) {
    FTPClient client = new FTPClient();

    FileInputStream fis = null;

    try {
        client.connect(FTPADDR);
        client.login(user, password);

        // Create an InputStream of the file to be uploaded

        String filename = FilenameOnLocalMachine;
        fis = new FileInputStream(filename);

        // Store file to server

        client.storeFile(PathOnFtp, 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:CFDI_Verification.CFDI_Verification.java

public static void list_directories(String directory) {
    String server = "192.1.1.64";
    String user = "ftpuser";
    String pass = "Oracle123";
    String result = "No Connected!";

    FTPClient ftpClient = new FTPClient();

    try {/*from   ww  w. j a v a 2 s.co  m*/
        ftpClient.connect(server);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.changeWorkingDirectory(directory);

        String[] directories = ftpClient.listNames();
        String dir;

        for (int i = 0; i < directories.length; i++) {
            System.out.println(directories[i]);
            dir = directories[i];
        }

        ftpClient.logout();
        ftpClient.disconnect();

    } catch (IOException ex) {
        System.out.println("Ooops! Error en conexin." + ex.getMessage());
    }
}

From source file:deincraftlauncher.IO.download.FTPSync.java

public static String[] listFiles(String dir) {

    FTPClient client = new FTPClient();

    try {//from   w  ww .  j a  v a 2s . c om
        client.connect(ftpServer);
        client.login(ftpUsername, ftpPassword);
        client.changeWorkingDirectory(dir);

        // Obtain a list of filenames in the current working
        // directory. When no file found an empty array will 
        // be returned.
        String[] names = client.listNames();

        client.logout();

        return names;

    } catch (IOException e) {
        System.err.println("Error connecting to ftp (listfiles): " + e);
    } finally {

        try {
            client.disconnect();
        } catch (IOException e) {
            System.err.println("Error disconnecting to ftp (listfiles): " + e);
        }
    }

    return null;

}

From source file:com.clothcat.hpoolauto.HtmlUploader.java

/**
 * Upload the html via FTP/*from w w  w . j  av  a2  s . c  o  m*/
 */
public static void uploadHtml() {
    FTPClient ftpc = new FTPClient();
    FTPClientConfig conf = new FTPClientConfig();
    ftpc.configure(conf);
    try {
        ftpc.connect(Constants.FTP_HOSTNAME);
        ftpc.login(Constants.FTP_USERNAME, Constants.FTP_PASSWORD);
        File dir = new File(Constants.HTML_FILEPATH);
        File[] files = dir.listFiles();
        ftpc.changeWorkingDirectory("/htdocs");
        for (File f : files) {
            HLogger.log(Level.FINEST, "Uploading file: " + f.getName());
            FileInputStream fis = new FileInputStream(f);
            ftpc.storeFile(f.getName(), fis);
        }
        ftpc.logout();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (ftpc.isConnected()) {
            try {
                ftpc.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:cn.zhuqi.mavenssh.web.util.test.FtpTest.java

/**
 * Description: ?FTP?/* ww  w. j  av a2  s  . c  o  m*/
 *
 * @param url FTP?hostname
 * @param port FTP???-1
 * @param username FTP?
 * @param password FTP?
 * @param path FTP??
 * @param filename FTP???
 * @param input ?
 * @return ?true?false
 */
public static boolean uploadFile(String url, int port, String username, String password, String path,
        String filename, InputStream input) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        // FTP?
        if (port > -1) {
            ftp.connect(url, port);
        } else {
            ftp.connect(url);
        }
        // FTP
        ftp.login(username, password);
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        ftp.changeWorkingDirectory(path);
        ftp.storeFile(filename, input);

        input.close();
        ftp.logout();
        success = true;
    } catch (IOException e) {
        success = false;
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException e) {
            }
        }
    }
    return success;
}

From source file:com.jason.thrift.TransmitHandler.java

/**
 * Description: ?FTP?/*w  ww  .ja va2  s .co m*/
 * 
 * @Version1.0 Jul 27, 2008 4:31:09 PM by ?cuihongbao@d-heaven.com
 * @param url
 *            FTP?hostname
 * @param port
 *            FTP??
 * @param username
 *            FTP?
 * @param password
 *            FTP?
 * @param path
 *            FTP??
 * @param filename
 *            FTP???
 * @param input
 *            ?
 * @return ?true?false
 */
public static boolean uploadFile(String url, int port, String username, String password, String path,
        String filename, InputStream input) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        ftp.connect(url, port);// FTP?
        // ??ftp.connect(url)?FTP?
        ftp.login(username, password);// 
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        ftp.changeWorkingDirectory(path);
        ftp.storeFile(filename, input);

        input.close();
        ftp.logout();
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
    return success;
}

From source file:de.jwi.ftp.FTPUploader.java

public static String upload(URL url, List files) {
    String rcs = "";

    if (!"ftp".equals(url.getProtocol())) {
        return "not ftp protocol";
    }//w  w  w.j  a  v  a  2 s.co  m

    String host = url.getHost();
    String userInfo = url.getUserInfo();
    String path = url.getPath();
    String user = null;
    String pass = null;

    int p;
    if ((userInfo != null) && ((p = userInfo.indexOf(':')) > -1)) {
        user = userInfo.substring(0, p);
        pass = userInfo.substring(p + 1);
    } else {
        user = userInfo;
    }

    FTPClient ftp = new FTPClient();

    try {
        int reply;
        ftp.connect(host);

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

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return "connection refused";
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
        return "could not connect to " + host;
    }

    try {
        if (!ftp.login(user, pass)) {
            ftp.logout();
            return "failed to login";
        }

        ftp.setFileType(FTP.BINARY_FILE_TYPE);

        // Use passive mode as default because most of us are
        // behind firewalls these days.
        ftp.enterLocalPassiveMode();

        rcs = uploadFiles(ftp, path, files);

        ftp.logout();
    } catch (FTPConnectionClosedException e) {
        return "connection closed";
    } catch (IOException e) {
        return e.getMessage();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }

    return rcs;
}