Example usage for org.apache.commons.net.ftp FTPSClient setDataTimeout

List of usage examples for org.apache.commons.net.ftp FTPSClient setDataTimeout

Introduction

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

Prototype

public void setDataTimeout(int timeout) 

Source Link

Document

Sets the timeout in milliseconds to use when reading from the data connection.

Usage

From source file:com.claim.controller.FileTransferController.java

public boolean uploadMutiFilesWithFTPS(ObjFileTransfer ftpObj) throws Exception {
    FTPSClient ftpsClient = null;
    int replyCode;
    boolean completed = false;
    try {/* w  w  w . j a v  a2 s  . com*/

        FtpProperties properties = new ResourcesProperties().loadFTPProperties();

        try {
            ftpsClient = new FTPSClient(properties.getFtp_protocal(), properties.isFtp_impicit());
            //ftpsClient.setAuthValue(ConstantFtp.FTPS_PROTOCAL);
            ftpsClient.setDataTimeout(ConstantFtp.FTP_TIMEOUT);

            ftpsClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
            System.out.print(ftpsClient.getReplyString());

            ftpsClient.connect(properties.getFtp_server(), properties.getFtp_port());
            FtpUtil.showServerReply(ftpsClient);

        } catch (ConnectException ex) {
            FtpUtil.showServerReply(ftpsClient);
            Console.LOG(ex.getMessage(), 0);
            System.out.println("ConnectException: " + ex.getMessage());
            ex.printStackTrace();
        } catch (SocketException ex) {
            FtpUtil.showServerReply(ftpsClient);
            Console.LOG(ex.getMessage(), 0);
            System.out.println("SocketException: " + ex.getMessage());
            ex.printStackTrace();
        } catch (UnknownHostException ex) {
            FtpUtil.showServerReply(ftpsClient);
            Console.LOG(ex.getMessage(), 0);
            System.out.println("UnknownHostException: " + ex.getMessage());
            ex.printStackTrace();
        }

        replyCode = ftpsClient.getReplyCode();
        FtpUtil.showServerReply(ftpsClient);

        if (!FTPReply.isPositiveCompletion(replyCode)) {
            ftpsClient.disconnect();
            Console.LOG("Exception in connecting to FTP Serve ", 0);
            throw new Exception("Exception in connecting to FTP Server");
        } else {
            Console.LOG("Success in connecting to FTP Serve ", 1);
        }

        try {
            boolean success = ftpsClient.login(properties.getFtp_username(), properties.getFtp_password());

            FtpUtil.showServerReply(ftpsClient);
            if (!success) {
                throw new Exception("Could not login to the FTP server.");
            } else {
                Console.LOG("login to the FTP server. Successfully ", 1);
            }
            //ftpClient.enterLocalPassiveMode();
        } catch (FTPConnectionClosedException ex) {
            Console.LOG(ex.getMessage(), 0);
            FtpUtil.showServerReply(ftpsClient);
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
        }

        ftpsClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpsClient.execPBSZ(0);

        //FTP_REMOTE_HOME = ftpClient.printWorkingDirectory();
        String workingDirectoryReportType = properties.getFtp_remote_directory() + File.separator
                + ftpObj.getFtp_report_type();
        FtpUtil.ftpCreateDirectoryTree(ftpsClient, workingDirectoryReportType);
        FtpUtil.showServerReply(ftpsClient);

        String workingDirectoryStmp = workingDirectoryReportType + File.separator + ftpObj.getFtp_stmp();
        FtpUtil.ftpCreateDirectoryTree(ftpsClient, workingDirectoryStmp);
        FtpUtil.showServerReply(ftpsClient);

        // APPROACH #2: uploads second file using an OutputStream
        File files = new File(ftpObj.getFtp_directory_path());

        for (File file : files.listFiles()) {
            if (file.isFile()) {
                System.out.println("file ::" + file.getName());
                InputStream in = new FileInputStream(file);
                ftpsClient.changeWorkingDirectory(workingDirectoryStmp);
                completed = ftpsClient.storeFile(file.getName(), in);
                in.close();
                Console.LOG(
                        "  " + file.getName() + " ",
                        1);
                FtpUtil.showServerReply(ftpsClient);
            }
        }
        Console.LOG(" ?... ", 1);

        //completed = ftpClient.completePendingCommand();
        FtpUtil.showServerReply(ftpsClient);
        completed = true;
        ftpsClient.disconnect();

    } catch (IOException ex) {
        Console.LOG(ex.getMessage(), 0);
        FtpUtil.showServerReply(ftpsClient);
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpsClient.isConnected()) {
                ftpsClient.logout();
                ftpsClient.disconnect();
            }
        } catch (IOException ex) {
            FtpUtil.showServerReply(ftpsClient);
            Console.LOG(ex.getMessage(), 0);
            ex.printStackTrace();
        }
    }
    return completed;
}

From source file:org.apache.camel.component.file.remote.FtpsEndpoint.java

@Override
public RemoteFileOperations<FTPFile> createRemoteFileOperations() throws Exception {
    // configure ftp client
    FTPSClient client = getFtpsClient();

    if (client == null) {
        // must use a new client if not explicit configured to use a custom client
        client = (FTPSClient) createFtpClient();
    }//from   w w w  .ja  v  a 2  s  .co m

    // set any endpoint configured timeouts
    if (getConfiguration().getConnectTimeout() > -1) {
        client.setConnectTimeout(getConfiguration().getConnectTimeout());
    }
    if (getConfiguration().getSoTimeout() > -1) {
        soTimeout = getConfiguration().getSoTimeout();
    }
    dataTimeout = getConfiguration().getTimeout();

    if (ftpClientParameters != null) {
        Map<String, Object> localParameters = new HashMap<String, Object>(ftpClientParameters);
        // setting soTimeout has to be done later on FTPClient (after it has connected)
        Object timeout = localParameters.remove("soTimeout");
        if (timeout != null) {
            soTimeout = getCamelContext().getTypeConverter().convertTo(int.class, timeout);
        }
        // and we want to keep data timeout so we can log it later
        timeout = localParameters.remove("dataTimeout");
        if (timeout != null) {
            dataTimeout = getCamelContext().getTypeConverter().convertTo(int.class, dataTimeout);
        }
        setProperties(client, localParameters);
    }

    if (ftpClientConfigParameters != null) {
        // client config is optional so create a new one if we have parameter for it
        if (ftpClientConfig == null) {
            ftpClientConfig = new FTPClientConfig();
        }
        Map<String, Object> localConfigParameters = new HashMap<String, Object>(ftpClientConfigParameters);
        setProperties(ftpClientConfig, localConfigParameters);
    }

    if (dataTimeout > 0) {
        client.setDataTimeout(dataTimeout);
    }

    if (log.isDebugEnabled()) {
        log.debug("Created FTPSClient [connectTimeout: {}, soTimeout: {}, dataTimeout: {}]: {}",
                new Object[] { client.getConnectTimeout(), getSoTimeout(), dataTimeout, client });
    }

    FtpsOperations operations = new FtpsOperations(client, getFtpClientConfig());
    operations.setEndpoint(this);
    return operations;
}

From source file:org.mule.transport.ftps.FtpsConnector.java

/**
 * Creates a new FTPSClient that logs in and changes the working directory using the data
 * provided in <code>endpoint</code>.
 *///  w w  w  . ja v  a  2s  .c om
protected FTPSClient createFTPSClient(ImmutableEndpoint endpoint) throws Exception {
    EndpointURI uri = endpoint.getEndpointURI();
    FTPSClient client = this.getFtp(uri);
    client.setDataTimeout(endpoint.getResponseTimeout());

    this.enterActiveOrPassiveMode(client, endpoint);
    this.setupFileType(client, endpoint);

    String path = uri.getPath();

    // only change directory if one was configured
    if (StringUtils.isNotBlank(path)) {
        // MULE-2400: if the path begins with '~' we must strip the first '/' to make things
        // work with FTPSClient
        if ((path.length() >= 2) && (path.charAt(1) == '~')) {
            path = path.substring(1);
        }

        //Checking if it is a file or a directory
        boolean isFile = this.isFile(endpoint, client);
        if (!isFile && !client.changeWorkingDirectory(path)) {
            throw new IOException(MessageFormat.format(
                    "Failed to change working directory to {0}. Ftp error: {1}", path, client.getReplyCode()));
        } else if (isFile) {
            // Changing the working directory to the parent folder, it should be better if
            // the FTPSClient API would provide a way to retrieve the parent folder
            FTPFile[] listFiles = client.listFiles(path);
            String directory = path.replaceAll(listFiles[0].getName(), "");
            client.changeWorkingDirectory(directory);
        }
    }
    return client;
}