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:com.globalsight.smartbox.util.FtpHelper.java

public boolean ftpDeleteFile(String p_fileName) {
    FTPClient ftpClient = getFtpClient();
    if (ftpClient != null && ftpClient.isConnected()) {
        try {/*from  ww  w  .  j ava 2  s.c om*/
            return ftpClient.deleteFile(p_fileName);
        } catch (IOException e) {
            LogUtil.fail("Delete File Error: " + p_fileName, e);
        } finally {
            closeFtpClient(ftpClient);
        }
    }

    return false;
}

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

public InputStream ftpDownloadFile(String p_remoteFileName) {
    InputStream is = null;/*from w  ww . ja  va  2 s .c  om*/
    FTPClient ftpClient = getFtpClient();
    if (ftpClient != null && ftpClient.isConnected()) {
        try {
            is = ftpClient.retrieveFileStream(p_remoteFileName);
        } catch (IOException e) {
            String message = "Failed to download file from InBox(in FTP), fileName: " + p_remoteFileName + ".";
            LogUtil.FAILEDLOG.error(message);
        } finally {
            closeFtpClient(ftpClient);
        }
    }

    return is;
}

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

/**
 * Downloads file from FTP to Locale.//from   ww  w. j av a  2s.  c o  m
 * 
 * @param p_remoteFileName
 *            File Name in FTP Server
 * @param p_localFileName
 *            File Name in Locale
 */
public void ftpDownloadFile(String p_remoteFileName, String p_localFileName) {
    FTPClient ftpClient = getFtpClient();
    if (ftpClient != null && ftpClient.isConnected()) {
        try {
            FileOutputStream local = new FileOutputStream(new File(p_localFileName));
            ftpClient.retrieveFile(p_remoteFileName, local);
            local.close();
        } catch (IOException e) {
            String message = "Failed to download file from InBox(in FTP), fileName: " + p_remoteFileName + ".";
            LogUtil.FAILEDLOG.error(message);
        } finally {
            closeFtpClient(ftpClient);
        }
    }
}

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

/**
 * Gets File Array in FTP Server by FTP Directory.
 * // ww  w.j a va 2  s  . c om
 * @param directory
 *            FTP Directory
 */
public FTPFile[] ftpFileList(String p_directory) {
    FTPFile[] result = {};
    FTPClient ftpClient = getFtpClient();
    if (ftpClient != null && ftpClient.isConnected()) {
        try {
            result = ftpClient.listFiles(p_directory);
        } catch (IOException e) {
            String message = "Ftp get name list error.";
            LogUtil.fail(message, e);
        } finally {
            closeFtpClient(ftpClient);
        }
    }

    return result;
}

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

public boolean ftpStoreFile(String remoteFileName, File localFile) {
    FTPClient ftpClient = getFtpClient();
    if (ftpClient != null && ftpClient.isConnected()) {
        FileInputStream localFIS = null;
        try {//from   ww  w  .  ja  v a2 s  .c  om
            localFIS = new FileInputStream(localFile);
            return ftpClient.storeFile(remoteFileName, localFIS);
        } catch (IOException e) {
            LogUtil.fail("Store File Error: " + remoteFileName, e);
        } finally {
            try {
                localFIS.close();
            } catch (IOException e) {
                LogUtil.fail("Fail to close FileInputStream: " + localFile.getAbsolutePath(), e);
            }
            closeFtpClient(ftpClient);
        }
    }

    return false;
}

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

public boolean testConnect() {
    FTPClient ftpClient = null;
    try {//from w w w . j  av a  2  s.  c  om
        ftpClient = getFtpClient();
        if (ftpClient != null && ftpClient.isConnected()) {
            return true;
        }
    } catch (Exception e) {
    } finally {
        closeFtpClient(ftpClient);
    }
    return false;
}

From source file:adams.core.io.lister.FtpDirectoryLister.java

/**
 * Disconnects the SSH session, if necessary.
 *
 * @param client   the client to disconnect
 *//* w  w w.  j a va2  s  . co  m*/
protected void disconnect(FTPClient client) {
    if (client != null) {
        if (client.isConnected()) {
            try {
                client.disconnect();
            } catch (Exception e) {
                Utils.handleException(this, "Failed to disconnect!", e);
            }
        }
    }
}

From source file:ca.nrc.cadc.web.ServerToServerFTPTransfer.java

public void send(final OutputStreamWrapper outputStreamWrapper, final String filename) throws IOException {
    final FTPClient ftpClient = connect();
    ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
    ftpClient.enterLocalPassiveMode();/*  w w w .ja va 2  s . c om*/

    try {
        final OutputStream outputStream = ftpClient.storeFileStream(filename);

        outputStreamWrapper.write(outputStream);

        outputStream.flush();
        outputStream.close();
    } finally {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }
}

From source file:ddf.test.itests.catalog.TestFtp.java

private void disconnectClient(FTPClient client) {
    if (client != null && client.isConnected()) {
        try {//w ww  . ja v  a 2s  . co  m
            client.logout();
        } catch (IOException ioe) {
            // ignore
        }
        try {
            client.disconnect();
        } catch (IOException ioe) {
            // ignore
        }
    }
}

From source file:com.cws.esolutions.core.utils.NetworkUtils.java

/**
 * Creates a connection to a target host and then executes an FTP
 * request to send or receive a file to or from the target. This is fully
 * key-based, as a result, a keyfile is required for the connection to
 * successfully authenticate./*  w ww  .  j  a v a  2s .  co m*/
 * 
 * @param sourceFile - The full path to the source file to transfer
 * @param targetFile - The full path (including file name) of the desired target file
 * @param targetHost - The target server to perform the transfer to
 * @param isUpload - <code>true</code> is the transfer is an upload, <code>false</code> if it
 *            is a download 
 * @throws UtilityException {@link com.cws.esolutions.core.utils.exception.UtilityException} if an error occurs processing
 */
public static final synchronized void executeFtpConnection(final String sourceFile, final String targetFile,
        final String targetHost, final boolean isUpload) throws UtilityException {
    final String methodName = NetworkUtils.CNAME
            + "#executeFtpConnection(final String sourceFile, final String targetFile, final String targetHost, final boolean isUpload) throws UtilityException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", sourceFile);
        DEBUGGER.debug("Value: {}", targetFile);
        DEBUGGER.debug("Value: {}", targetHost);
        DEBUGGER.debug("Value: {}", isUpload);
    }

    final FTPClient client = new FTPClient();
    final FTPConfig ftpConfig = appBean.getConfigData().getFtpConfig();

    if (DEBUG) {
        DEBUGGER.debug("FTPClient: {}", client);
        DEBUGGER.debug("FTPConfig: {}", ftpConfig);
    }

    try {
        client.connect(targetHost);

        if (DEBUG) {
            DEBUGGER.debug("FTPClient: {}", client);
        }

        if (!(client.isConnected())) {
            throw new IOException("Failed to authenticate to remote host with the provided information");
        }

        boolean isAuthenticated = false;

        if (StringUtils.isNotBlank(ftpConfig.getFtpAccount())) {
            isAuthenticated = client.login(
                    (StringUtils.isNotEmpty(ftpConfig.getFtpAccount())) ? ftpConfig.getFtpAccount()
                            : System.getProperty("user.name"),
                    PasswordUtils.decryptText(ftpConfig.getFtpPassword(), ftpConfig.getFtpSalt(),
                            secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(),
                            secBean.getConfigData().getSecurityConfig().getIterations(),
                            secBean.getConfigData().getSecurityConfig().getKeyBits(),
                            secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(),
                            secBean.getConfigData().getSecurityConfig().getEncryptionInstance(),
                            appBean.getConfigData().getSystemConfig().getEncoding()));
        } else {
            isAuthenticated = client.login(ftpConfig.getFtpAccount(), null);
        }

        if (DEBUG) {
            DEBUGGER.debug("isAuthenticated: {}", isAuthenticated);
        }

        if (!(isAuthenticated)) {
            throw new IOException("Failed to connect to FTP server: " + targetHost);
        }

        client.enterLocalPassiveMode();

        if (!(FileUtils.getFile(sourceFile).exists())) {
            throw new IOException("File " + sourceFile + " does not exist. Skipping");
        }

        if (isUpload) {
            client.storeFile(targetFile, new FileInputStream(FileUtils.getFile(sourceFile)));
        } else {
            client.retrieveFile(sourceFile, new FileOutputStream(targetFile));
        }

        if (DEBUG) {
            DEBUGGER.debug("Reply: {}", client.getReplyCode());
            DEBUGGER.debug("Reply: {}", client.getReplyString());
        }
    } catch (IOException iox) {
        throw new UtilityException(iox.getMessage(), iox);
    } finally {
        try {
            if (client.isConnected()) {
                client.completePendingCommand();
                client.disconnect();
            }
        } catch (IOException iox) {
            ERROR_RECORDER.error(iox.getMessage(), iox);
        }
    }
}