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:net.audumla.climate.bom.BOMDataLoader.java

private synchronized FTPClient getFTPClient(String host) {
    FTPClient ftp = ftpClients.get(host);
    if (ftp == null || !ftp.isAvailable() || !ftp.isConnected()) {
        ftp = new FTPClient();
        FTPClientConfig config = new FTPClientConfig();
        ftp.configure(config);//from   w  w  w. j  av  a2  s  .  c o m
        try {
            ftp.setControlKeepAliveTimeout(30);
            ftp.setControlKeepAliveReplyTimeout(5);
            ftp.setDataTimeout(3000);
            ftp.setDefaultTimeout(1000);
            int reply;
            ftp.connect(host);
            LOG.debug("Connected to " + host);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                LOG.error("FTP server '" + host + "' refused connection.");
            } else {
                if (!ftp.login("anonymous", "guest")) {
                    LOG.error("Unable to login to server " + host);
                }
                ftp.setSoTimeout(60000);
                ftp.enterLocalPassiveMode();
                ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

            }
        } catch (IOException e) {
            LOG.error("Unable to connect to " + host, e);
        }
        ftpClients.put(host, ftp);
    }
    if (!ftp.isConnected() || !ftp.isAvailable()) {
        throw new UnsupportedOperationException("Cannot connect to " + host);
    }
    return ftp;
}

From source file:net.paissad.jcamstream.utils.FTPUtils.java

/**
 * Logout and then disconnect from the FTP server.
 * // w ww .j  a v a 2 s .  c om
 * @throws IOException
 * 
 * @see org.apache.commons.net.ftp.FTPClient#logout()
 * @see org.apache.commons.net.ftp.FTPClient#disconnect()
 */
public void logoutAndDisconnect() throws IOException {
    FTPClient client = this.getFtpClient();

    if (client.isConnected() && !FTPReply.isPositiveIntermediate(client.getReplyCode())) {
        client.logout();
        client.disconnect();
        System.out.println("Disconnected successfully from FTP server.");
    }

    if (client.isConnected() && !client.completePendingCommand()) {
        System.err.println("Something failed !");
    }
}

From source file:autonomouspathplanner.ftp.FTP.java

/**
 * Attempts to connect to server to check if it is possible
 * @return true if the client can connect to the server and false otherwise.
 *//* www.  jav a 2  s. c  om*/
public boolean canConnect() {
    try {
        FTPClient c = new FTPClient();
        c.connect(IP, port);

        c.login(user, pass);
        if (c.isConnected()) {
            c.logout();
            c.disconnect();
            return true;
        } else
            return false;
    } catch (UnknownHostException x) {
        return false;
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    }

}

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

@Override
public void mouseClicked(MouseEvent e) {
    CrawlerResultsItem.PreviewPanel previewPanel = (CrawlerResultsItem.PreviewPanel) e.getSource();
    CrawlerResult crawlerResult = previewPanel.getCrawlerResult();
    CrawlerFile crawlerFile = crawlerResult.getFile();

    FTPClient ftpClient = crawlerResult.getFtpClient();

    if (ftpClient.isConnected()) {
        JOptionPane.showMessageDialog(portalFrame, i18n.translate("crawlerDownloadWhileConnected"), null,
                JOptionPane.ERROR_MESSAGE);
        return;/*from  ww w  .  ja  v  a 2 s.  c  om*/
    }

    String fileExtension = crawlerFile.getExtension();

    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter chooserFilter = new FileNameExtensionFilter(
            i18n.translate("fileType", fileExtension.toUpperCase()), crawlerFile.getExtension());

    chooser.setApproveButtonText(i18n.translate("buttonSave"));
    chooser.setDialogTitle(i18n.translate("fileDownloadTo"));
    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    chooser.setFileFilter(chooserFilter);
    chooser.setSelectedFile(new File(crawlerFile.getName()));

    int selection = chooser.showSaveDialog(portalFrame);

    if (selection == JFileChooser.APPROVE_OPTION) {
        File fileToSave = chooser.getSelectedFile();

        Server relatedServer = crawlerResult.getServer();

        try {
            ftpClient.connect(relatedServer.need("server.ip"),
                    Integer.parseInt(relatedServer.need("server.port")));

            int replyCode = ftpClient.getReplyCode();

            if (!FTPReply.isPositiveCompletion(replyCode)) {
                throw new IOException(i18n.translate("crawlerServerRefused"));
            }

            if (relatedServer.has("user.name")) {
                String userName = relatedServer.get("user.name");
                String userPassword = "";

                if (relatedServer.hasTemporary("user.password")) {
                    userPassword = relatedServer.getTemporary("user.password");
                }

                boolean loggedIn = ftpClient.login(userName, userPassword);

                if (!loggedIn) {
                    throw new IOException(i18n.translate("crawlerServerAuthFail"));
                }
            }

            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            /* Download file */

            InputStream is = ftpClient.retrieveFileStream(crawlerFile.getFullName());

            if (is != null) {

                byte[] rawFile = new byte[(int) crawlerFile.getSize()];

                int i = 0;

                while (true) {
                    int b = is.read();

                    if (b == -1) {
                        break;
                    }

                    rawFile[i] = (byte) b;
                    i++;

                    /* Occasionally update the download progress */

                    if (i % 1024 == 0) {
                        int progress = Math.round((((float) i) / crawlerFile.getSize()) * 100);

                        status.add(i18n.translate("crawlerDownloadProgress", progress));
                    }
                }

                is.close();
                is = null;

                if (!ftpClient.completePendingCommand()) {
                    throw new IOException();
                }

                Files.write(fileToSave.toPath(), rawFile);
            }

            /* Logout and disconnect */

            ftpClient.logout();

            tryDisconnect(ftpClient);

            status.add(i18n.translate("crawlerDownloadDone"));
        } catch (IOException ex) {
            tryDisconnect(ftpClient);

            JOptionPane.showMessageDialog(portalFrame, i18n.translate("crawlerDownloadFailed", ex.getMessage()),
                    null, JOptionPane.ERROR_MESSAGE);
        }
    }
}

From source file:com.jaeksoft.searchlib.crawler.file.process.fileInstances.FtpFileInstance.java

private void ftpQuietDisconnect(FTPClient f) {
    if (f == null)
        return;/*w w  w  . ja  v  a  2  s.c om*/
    try {
        if (f.isConnected())
            f.disconnect();
    } catch (IOException e) {
        Logging.warn(e);
    }
}

From source file:com.jaeksoft.searchlib.scheduler.task.TaskFtpXmlFeed.java

private void checkConnect(FTPClient ftp, String server, String login, String password) throws IOException {
    try {//from w  ww.j  a  v  a 2s . c o  m
        if (ftp.isConnected())
            if (ftp.sendNoOp())
                return;
    } catch (FTPConnectionClosedException e) {
        Logging.warn(e);
    }
    ftp.setConnectTimeout(120000);
    ftp.setControlKeepAliveTimeout(180);
    ftp.setDataTimeout(120000);
    ftp.connect(server);
    ftp.login(login, password);
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

public static void uploadWebConfig(WebApp webApp, InputStream fileStream, IProgressIndicator indicator)
        throws IOException {
    FTPClient ftp = null;
    try {/*from   w ww .j  ava2s. c o m*/
        PublishingProfile pp = webApp.getPublishingProfile();
        ftp = getFtpConnection(pp);

        if (indicator != null)
            indicator.setText("Stopping the service...");
        webApp.stop();

        if (indicator != null)
            indicator.setText("Uploading " + webConfigFilename + "...");
        ftp.storeFile(ftpRootPath + webConfigFilename, fileStream);

        if (indicator != null)
            indicator.setText("Starting the service...");
        webApp.start();
    } finally {
        if (ftp != null && ftp.isConnected()) {
            ftp.disconnect();
        }
    }
}

From source file:com.globalsight.smartbox.util.FtpHelper.java

public boolean ftpCreateDir(String p_path) {
    FTPClient ftpClient = getFtpClient();
    if (ftpClient != null && ftpClient.isConnected()) {
        try {/*from w w w.j a v a 2s.  co  m*/
            return ftpClient.makeDirectory(p_path);
        } catch (IOException e) {
            LogUtil.fail("Create Directory error by path:" + p_path, e);
        } finally {
            closeFtpClient(ftpClient);
        }
    }

    return false;
}

From source file:com.globalsight.smartbox.util.FtpHelper.java

public boolean ftpDirExists(String p_path) {
    FTPClient ftpClient = getFtpClient();
    if (ftpClient != null && ftpClient.isConnected()) {
        try {/*ww  w  . j  a  va2  s.  c  o m*/
            return ftpClient.changeWorkingDirectory(p_path);
        } catch (Exception e) {
            return false;
        } finally {
            closeFtpClient(ftpClient);
        }
    }

    return true;
}

From source file:com.globalsight.smartbox.util.FtpHelper.java

public boolean ftpRename(String p_target, String p_file) {
    FTPClient ftpClient = getFtpClient();
    if (ftpClient != null && ftpClient.isConnected()) {
        try {/* w w  w .j  a va 2 s  .  c  o  m*/
            ftpClient.rename(p_file, p_target);
        } catch (IOException e) {
            return false;
        } finally {
            closeFtpClient(ftpClient);
        }
    }

    return true;
}