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

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

Introduction

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

Prototype

public boolean isConnected() 

Source Link

Document

Returns true if the client is currently connected to a server.

Usage

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.ja  v  a2s. c o 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:com.savy3.util.MainframeFTPClientUtils.java

public static boolean closeFTPConnection(FTPClient ftp) {
    boolean success = true;
    try {/*from   w w  w .  j  ava2 s .  c  o  m*/
        ftp.noop(); // check that control connection is working OK
        ftp.logout();
    } catch (FTPConnectionClosedException e) {
        success = false;
        LOG.warn("Server closed connection: " + e.toString());
    } catch (IOException e) {
        success = false;
        LOG.warn("Server closed connection: " + e.toString());
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                success = false;
            }
        }
    }
    return success;
}

From source file:madkitgroupextension.export.Export.java

private static void sendToWebSite() throws IOException {
    System.out.println("Enter your password :");
    byte b[] = new byte[100];
    int l = System.in.read(b);
    String pwd = new String(b, 0, l);

    boolean reconnect = true;
    long time = System.currentTimeMillis();
    File current_file_transfert = null;
    File current_directory_transfert = null;

    while (reconnect) {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(FTPURL, 21);/*from  w w  w.  java2  s  . co m*/
        try {
            if (ftpClient.isConnected()) {
                System.out.println("Connected to server " + FTPURL + " (Port: " + FTPPORT + ") !");
                if (ftpClient.login(FTPLOGIN, pwd)) {
                    ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
                    System.out.println("Logged as " + FTPLOGIN + " !");
                    System.out.print("Updating...");

                    FTPFile files[] = ftpClient.listFiles("");
                    FTPFile downloadroot = null;
                    FTPFile docroot = null;

                    for (FTPFile f : files) {
                        if (f.getName().equals("downloads")) {
                            downloadroot = f;
                            if (docroot != null)
                                break;
                        }
                        if (f.getName().equals("doc")) {
                            docroot = f;
                            if (downloadroot != null)
                                break;
                        }
                    }
                    if (downloadroot == null) {
                        //ftpClient.changeWorkingDirectory("/");
                        if (!ftpClient.makeDirectory("downloads")) {
                            System.err.println("Impossible to create directory: downloads");
                        }
                    }
                    if (docroot == null) {
                        //ftpClient.changeWorkingDirectory("/");
                        if (!ftpClient.makeDirectory("doc")) {
                            System.err.println("Impossible to create directory: doc");
                        }
                    }

                    updateFTP(ftpClient, "downloads/", new File(ExportPathFinal), current_file_transfert,
                            current_directory_transfert);
                    updateFTP(ftpClient, "doc/", new File("./doc"), current_file_transfert,
                            current_directory_transfert);
                    reconnect = false;

                    System.out.println("[OK]");
                    if (ftpClient.logout()) {
                        System.out.println("Logged out from " + FTPLOGIN + " succesfull !");
                    } else
                        System.err.println("Logged out from " + FTPLOGIN + " FAILED !");
                } else
                    System.err.println("Impossible to log as " + FTPLOGIN + " !");

                ftpClient.disconnect();
                System.out.println("Disconnected from " + FTPURL + " !");
            } else {
                System.err.println("Impossible to get a connection to the server " + FTPURL + " !");
            }
            reconnect = false;
        } catch (TransfertException e) {
            if (System.currentTimeMillis() - time > 30000) {
                System.err.println("A problem occured during the transfert...");
                System.out.println("Reconnection in progress...");
                try {
                    ftpClient.disconnect();
                } catch (Exception e2) {
                }
                current_file_transfert = e.current_file_transfert;
                current_directory_transfert = e.current_directory_transfert;
                time = System.currentTimeMillis();
            } else {
                System.err.println("A problem occured during the transfert. Transfert aborded.");
                throw e.original_exception;
            }
        }
    }
}

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

/**
 * Upload the html via FTP/* w  ww .jav a  2  s . co 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?/*from  w w  w .j  av a2s  . co 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 w w .j a  v a 2  s.c  o  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:gui.TesteHackathon.java

public static void enviaImagem() {
    String server = "www.ejec.co";
    int port = 21;
    String user = "ejec";
    String pass = "cPanel2015";

    FTPClient ftpClient = new FTPClient();
    try {/*w  w  w  .  j  a v a 2 s .co  m*/

        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        // APPROACH #1: uploads first file using an InputStream
        File firstLocalFile = new File("image.jpg");
        String firstRemoteFile = "public_html/virtualfit/image.jpg";
        InputStream inputStream = new FileInputStream(firstLocalFile);

        System.out.println("Start uploading first file");
        boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
        inputStream.close();
        if (done) {
            System.out.println("The first file is uploaded successfully.");
        }

    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:lucee.runtime.net.ftp.FTPPoolImpl.java

/**
 * disconnect a client//w w  w .  ja  v  a2  s .  co  m
 * @param client
 */
private void disconnect(FTPClient client) {
    try {
        if (client != null && client.isConnected()) {
            client.quit();
            client.disconnect();
        }
    } catch (IOException ioe) {
    }
}

From source file:de.ep3.ftpc.controller.portal.CrawlerDownloadController.java

private void tryDisconnect(FTPClient ftpClient) {
    if (ftpClient.isConnected()) {
        try {/*from  www  . ja v  a2  s. c o  m*/
            ftpClient.disconnect();
        } catch (IOException e) {
        }
    }
}

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

/**
 * Description: FTP?/*from w ww  .  j  a  v  a2s  . c  o  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;
}