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:mysynopsis.FTPUploader.java

public static String uploadWizard() throws IOException {

    FTPClient connect;
    connect = null;//from   ww w .  j av  a  2 s  . c o m

    try {
        connect = new FTPClient();
        connect.connect(server, 21);
        int reply = connect.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            connect.disconnect();
            return "Can't Connect to the Server";
        }

        boolean check = connect.login(user, pass);
        if (!check) {
            return "Username or password Incorrect";
        }

        connect.setFileType(FTP.BINARY_FILE_TYPE);
        connect.enterLocalPassiveMode();

        InputStream input;
        try {
            input = new FileInputStream(new File("index.html"));
            connect.storeFile(dir + "index.html", input);
            input.close();
            connect.logout();
            connect.disconnect();

        } catch (IOException ex) {
            return "You need to put a slash (/) at the end";
        }
    } catch (IOException iOException) {
        return "Wrong Server Information. Please Try again";
    }
    return "File Transfer Successful!";
}

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";
    }/*from w w w .  j a  v  a 2s. 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;
}

From source file:ftp.FTPtask.java

/**
 * ? ? ? ??      Folder//from  ww w.  ja v a 2  s .c  o  m
 * @param FTPADDR, ?  ?
 * @param user,   
 * @param password,   
 * @param Folder,    ?? ?
 * @return String[] result, ?? /
 */

public static String[] GetList(String FTPADDR, String user, String password, String Folder) {
    String[] result;
    result = null;

    FTPClient client = new FTPClient();

    try {
        client.connect(FTPADDR);
        client.login(user, password);
        client.changeWorkingDirectory(Folder);
        result = client.listNames();
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:CFDI_Verification.CFDI_Verification.java

public static ARRAY getDirectories(String directory) throws SQLException {
    Connection conn = new OracleDriver().defaultConnection();
    ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("DIRECTORIES_LIST", conn);

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

    FTPClient ftpClient = new FTPClient();

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

        String[] directories = ftpClient.listNames();
        ARRAY directories_list = new ARRAY(descriptor, conn, directories);

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

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

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  w  w . ja  v  a 2 s . c om*/
        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 a 2 s .c om
 */
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
 *  // ww  w  .  j a v  a2s.c  om
 * @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:cn.zhuqi.mavenssh.web.util.test.FtpTest.java

/**
 * Description: FTP?//from w w  w  . j a va2  s .co  m
 *
 * @Version1.0
 * @param url FTP?hostname
 * @param port FTP??
 * @param username FTP?
 * @param password FTP?
 * @param remotePath FTP?
 * @param fileName ???
 * @param localPath ??
 * @return
 */
public static boolean downloadFile(String url, int port, String username, String password, String remotePath,
        String fileName, String localPath) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        // FTP?
        if (port > -1) {
            ftp.connect(url, port);
        } else {
            ftp.connect(url);
        }
        ftp.login(username, password);//
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        ftp.changeWorkingDirectory(remotePath);//FTP?
        FTPFile[] fs = ftp.listFiles();
        for (FTPFile ff : fs) {
            if (ff.getName().equals(fileName)) {
                File localFile = new File(localPath + "/" + ff.getName());
                OutputStream is = new FileOutputStream(localFile);
                ftp.retrieveFile(ff.getName(), is);
                is.close();
            }
        }

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

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 {/*ww  w .j  a  v a  2s. c o 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:com.microsoft.azure.management.appservice.samples.ManageWebAppSourceControl.java

private static void uploadFileToFtp(PublishingProfile profile, String fileName, InputStream file)
        throws Exception {
    FTPClient ftpClient = new FTPClient();
    String[] ftpUrlSegments = profile.ftpUrl().split("/", 2);
    String server = ftpUrlSegments[0];
    String path = "./site/wwwroot/webapps";
    ftpClient.connect(server);//from   w  w w  . j a  v  a  2  s  . c om
    ftpClient.login(profile.ftpUsername(), profile.ftpPassword());
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClient.changeWorkingDirectory(path);
    ftpClient.storeFile(fileName, file);
    ftpClient.disconnect();
}