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

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

Introduction

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

Prototype

public void setBufferSize(int bufSize) 

Source Link

Document

Set the internal buffer size for buffered data streams.

Usage

From source file:net.yacy.grid.io.assets.FTPStorageFactory.java

public FTPStorageFactory(String server, int port, String username, String password, boolean deleteafterread)
        throws IOException {
    this.server = server;
    this.username = username == null ? "" : username;
    this.password = password == null ? "" : password;
    this.port = port;
    this.deleteafterread = deleteafterread;

    this.ftpClient = new Storage<byte[]>() {

        @Override//from  w  w  w. j a  va 2s  .co  m
        public void checkConnection() throws IOException {
            return;
        }

        private FTPClient initConnection() throws IOException {
            FTPClient ftp = new FTPClient();
            ftp.setDataTimeout(3000);
            ftp.setConnectTimeout(20000);
            if (FTPStorageFactory.this.port < 0 || FTPStorageFactory.this.port == DEFAULT_PORT) {
                ftp.connect(FTPStorageFactory.this.server);
            } else {
                ftp.connect(FTPStorageFactory.this.server, FTPStorageFactory.this.port);
            }
            ftp.enterLocalPassiveMode(); // the server opens a data port to which the client conducts data transfers
            int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                if (ftp != null)
                    try {
                        ftp.disconnect();
                    } catch (Throwable ee) {
                    }
                throw new IOException("bad connection to ftp server: " + reply);
            }
            if (!ftp.login(FTPStorageFactory.this.username, FTPStorageFactory.this.password)) {
                if (ftp != null)
                    try {
                        ftp.disconnect();
                    } catch (Throwable ee) {
                    }
                throw new IOException("login failure");
            }
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.setBufferSize(8192);
            return ftp;
        }

        @Override
        public StorageFactory<byte[]> store(String path, byte[] asset) throws IOException {
            long t0 = System.currentTimeMillis();
            FTPClient ftp = initConnection();
            try {
                long t1 = System.currentTimeMillis();
                String file = cdPath(ftp, path);
                long t2 = System.currentTimeMillis();
                ftp.enterLocalPassiveMode();
                boolean success = ftp.storeFile(file, new ByteArrayInputStream(asset));
                long t3 = System.currentTimeMillis();
                if (!success)
                    throw new IOException("storage to path " + path + " was not successful (storeFile=false)");
                Data.logger.debug("FTPStorageFactory.store ftp store successfull: check connection = "
                        + (t1 - t0) + ", cdPath = " + (t2 - t1) + ", store = " + (t3 - t2));
            } catch (IOException e) {
                throw e;
            } finally {
                if (ftp != null)
                    try {
                        ftp.disconnect();
                    } catch (Throwable ee) {
                    }
            }
            return FTPStorageFactory.this;
        }

        @Override
        public Asset<byte[]> load(String path) throws IOException {
            FTPClient ftp = initConnection();
            ByteArrayOutputStream baos = null;
            byte[] b = null;
            try {
                String file = cdPath(ftp, path);
                baos = new ByteArrayOutputStream();
                ftp.retrieveFile(file, baos);
                b = baos.toByteArray();
                if (FTPStorageFactory.this.deleteafterread)
                    try {
                        boolean deleted = ftp.deleteFile(file);
                        FTPFile[] remaining = ftp.listFiles();
                        if (remaining.length == 0) {
                            ftp.cwd("/");
                            if (path.startsWith("/"))
                                path = path.substring(1);
                            int p = path.indexOf('/');
                            if (p > 0)
                                path = path.substring(0, p);
                            ftp.removeDirectory(path);
                        }
                    } catch (Throwable e) {
                        Data.logger.warn("FTPStorageFactory.load failed to remove asset " + path, e);
                    }
            } catch (IOException e) {
                throw e;
            } finally {
                if (ftp != null)
                    try {
                        ftp.disconnect();
                    } catch (Throwable ee) {
                    }
            }
            return new Asset<byte[]>(FTPStorageFactory.this, b);
        }

        @Override
        public void close() {
        }

        private String cdPath(FTPClient ftp, String path) throws IOException {
            int success_code = ftp.cwd("/");
            if (success_code >= 300)
                throw new IOException("cannot cd into " + path + ": " + success_code);
            if (path.length() == 0 || path.equals("/"))
                return "";
            if (path.charAt(0) == '/')
                path = path.substring(1); // we consider that all paths are absolute to / (home)
            int p;
            while ((p = path.indexOf('/')) > 0) {
                String dir = path.substring(0, p);
                int code = ftp.cwd(dir);
                if (code >= 300) {
                    // path may not exist, try to create the path
                    boolean success = ftp.makeDirectory(dir);
                    if (!success)
                        throw new IOException("unable to create directory " + dir + " for path " + path);
                    code = ftp.cwd(dir);
                    if (code >= 300)
                        throw new IOException("unable to cwd into directory " + dir + " for path " + path);
                }
                path = path.substring(p + 1);
            }
            return path;
        }
    };
}

From source file:org.apache.hadoop.fs.ftp.FTPFileSystem.java

/**
 * Connect to the FTP server using configuration parameters *
 * /*ww  w .  jav  a2  s  .  com*/
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
    FTPClient client = null;
    Configuration conf = getConf();
    String host = conf.get("fs.ftp.host");
    int port = conf.getInt("fs.ftp.host.port", FTP.DEFAULT_PORT);
    String user = conf.get("fs.ftp.user." + host);
    String password = conf.get("fs.ftp.password." + host);
    client = new FTPClient();
    client.connect(host, port);
    int reply = client.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        throw new IOException("Server - " + host + " refused connection on port - " + port);
    } else if (client.login(user, password)) {
        client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
        client.setFileType(FTP.BINARY_FILE_TYPE);
        client.setBufferSize(DEFAULT_BUFFER_SIZE);
    } else {
        throw new IOException("Login failed on server - " + host + ", port - " + port);
    }

    return client;
}

From source file:org.pepstock.jem.node.resources.impl.ftp.FtpFactory.java

/**
 * Creates and configures a FtpClient instance based on the
 * given properties./*from  w  w w .j a v  a 2 s .  c o  m*/
 * 
 * @param properties the ftp client configuration properties
 * @return remote input/output steam
 * @throws JNDIException if an error occurs creating the ftp client
 */
private Object createFtpClient(Properties properties) throws JNDIException {
    // URL is mandatory
    String ftpUrlString = properties.getProperty(CommonKeys.URL);
    if (ftpUrlString == null) {
        throw new JNDIException(NodeMessage.JEMC136E, CommonKeys.URL);
    }

    // creates URL objects
    // from URL string
    URL ftpUrl;
    try {
        ftpUrl = new URL(ftpUrlString);
    } catch (MalformedURLException e) {
        throw new JNDIException(NodeMessage.JEMC233E, e, ftpUrlString);
    }
    // checks scheme
    // if SSL, activates a FTPS
    if (!ftpUrl.getProtocol().equalsIgnoreCase(FTP_PROTOCOL)
            && !ftpUrl.getProtocol().equalsIgnoreCase(FTPS_PROTOCOL)) {
        throw new JNDIException(NodeMessage.JEMC137E, ftpUrl.getProtocol());
    }

    // gets port the host (from URL)
    int port = ftpUrl.getPort();
    String server = ftpUrl.getHost();

    // User id is mandatory
    String username = properties.getProperty(CommonKeys.USERID);
    if (username == null) {
        throw new JNDIException(NodeMessage.JEMC136E, CommonKeys.USERID);
    }

    // password is mandatory
    String password = properties.getProperty(CommonKeys.PASSWORD);
    if (password == null) {
        throw new JNDIException(NodeMessage.JEMC136E, CommonKeys.PASSWORD);
    }

    // checks if as input stream or not
    boolean asInputStream = Parser
            .parseBoolean(properties.getProperty(FtpResourceKeys.AS_INPUT_STREAM, "false"), false);

    String remoteFile = null;
    String accessMode = null;
    if (asInputStream) {
        // remote file is mandatory
        // it must be set by a data description
        remoteFile = properties.getProperty(FtpResourceKeys.REMOTE_FILE);
        if (remoteFile == null) {
            throw new JNDIException(NodeMessage.JEMC136E, FtpResourceKeys.REMOTE_FILE);
        }
        // access mode is mandatory
        // it must be set by a data description
        accessMode = properties.getProperty(FtpResourceKeys.ACTION_MODE, FtpResourceKeys.ACTION_READ);
    }

    // creates a FTPclient 
    FTPClient ftp = ftpUrl.getProtocol().equalsIgnoreCase(FTP_PROTOCOL) ? new FTPClient() : new FTPSClient();

    // checks if binary
    boolean binaryTransfer = Parser.parseBoolean(properties.getProperty(FtpResourceKeys.BINARY, "false"),
            false);

    // checks if must be restarted
    long restartOffset = Parser.parseLong(properties.getProperty(FtpResourceKeys.RESTART_OFFSET, "-1"), -1);

    // checks and sets buffer size
    int bufferSize = Parser.parseInt(properties.getProperty(FtpResourceKeys.BUFFER_SIZE, "-1"), -1);

    try {
        // reply code instance
        int reply;
        // connect to server
        if (port > 0) {
            ftp.connect(server, port);
        } else {
            ftp.connect(server);
        }

        // After connection attempt, you should check the reply code to
        // verify
        // success.
        reply = ftp.getReplyCode();
        // if not connected for error, EXCEPTION
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new JNDIException(NodeMessage.JEMC138E, reply);
        }
        // login!!
        if (!ftp.login(username, password)) {
            ftp.logout();
        }
        // set all ftp properties
        if (binaryTransfer) {
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
        }

        // sets restart offset if has been set
        if (restartOffset >= 0) {
            ftp.setRestartOffset(restartOffset);
        }

        // sets buffer size
        if (bufferSize >= 0) {
            ftp.setBufferSize(bufferSize);
        }

        // if is not related to a data descritpion,
        // returns a FTP object
        if (!asInputStream) {
            return new Ftp(ftp);
        }

        // checks if is in WRITE mode
        if (accessMode.equalsIgnoreCase(FtpResourceKeys.ACTION_WRITE)) {
            // gets outputstream
            // using the file name passed by data descritpion
            OutputStream os = ftp.storeFileStream(remoteFile);
            if (os == null) {
                reply = ftp.getReplyCode();
                throw new JNDIException(NodeMessage.JEMC206E, remoteFile, reply);
            }
            // returns outputstream
            return new FtpOutputStream(os, ftp);
        } else {
            // gest inputstream
            // using the file name passed by data descritpion
            InputStream is = ftp.retrieveFileStream(remoteFile);
            if (is == null) {
                reply = ftp.getReplyCode();
                throw new JNDIException(NodeMessage.JEMC206E, remoteFile, reply);
            }
            // returns inputstream 
            return new FtpInputStream(is, ftp);
        }
    } catch (SocketException e) {
        throw new JNDIException(NodeMessage.JEMC234E, e);
    } catch (IOException e) {
        throw new JNDIException(NodeMessage.JEMC234E, e);
    }
}

From source file:org.teiid.resource.adapter.ftp.FtpManagedConnectionFactory.java

private void afterConnectProcessing(FTPClient client) throws IOException {

    if (this.parentDirectory == null) {
        throw new IOException(UTIL.getString("parentdirectory_not_set")); //$NON-NLS-1$
    }//from  ww w  .  j ava 2 s  .  co m

    if (!client.changeWorkingDirectory(this.getParentDirectory())) {
        throw new IOException(UTIL.getString("ftp_dir_not_exist", this.getParentDirectory())); //$NON-NLS-1$
    }

    updateClientMode(client);

    client.setFileType(this.fileType);
    client.setBufferSize(this.bufferSize);

    if (this.isFtps) {
        FTPSClient ftpsClient = (FTPSClient) client;
        if (this.getAuthValue() != null) {
            ftpsClient.setAuthValue(this.authValue);
        }
        if (this.trustManager != null) {
            ftpsClient.setTrustManager(this.trustManager);
        }
        if (this.cipherSuites != null) {
            ftpsClient.setEnabledCipherSuites(this.cipherSuites);
        }
        if (this.protocols != null) {
            ftpsClient.setEnabledProtocols(this.protocols);
        }
        if (this.sessionCreation != null) {
            ftpsClient.setEnabledSessionCreation(this.sessionCreation);
        }
        if (this.useClientMode != null) {
            ftpsClient.setUseClientMode(this.useClientMode);
        }
        if (this.sessionCreation != null) {
            ftpsClient.setEnabledSessionCreation(this.sessionCreation);
        }
        if (this.keyManager != null) {
            ftpsClient.setKeyManager(this.keyManager);
        }
        if (this.needClientAuth != null) {
            ftpsClient.setNeedClientAuth(this.needClientAuth);
        }
        if (this.wantsClientAuth != null) {
            ftpsClient.setWantClientAuth(this.wantsClientAuth);
        }
    }
}

From source file:org.teiid.test.teiid4441.FTPClientFactory.java

private void afterConnectProcessing(FTPClient client) throws IOException {

    if (this.parentDirectory == null) {
        throw new IOException("parentdirectory_not_set");
    }//ww w .  ja v a2 s .c  om

    if (!client.changeWorkingDirectory(this.getParentDirectory())) {
        throw new IOException("ftp_dir_not_exist");
    }

    updateClientMode(client);

    client.setFileType(this.fileType);
    client.setBufferSize(this.bufferSize);

    if (this.isFtps) {
        FTPSClient ftpsClient = (FTPSClient) client;
        if (this.getAuthValue() != null) {
            ftpsClient.setAuthValue(this.authValue);
        }
        if (this.trustManager != null) {
            ftpsClient.setTrustManager(this.trustManager);
        }
        if (this.cipherSuites != null) {
            ftpsClient.setEnabledCipherSuites(this.cipherSuites);
        }
        if (this.protocols != null) {
            ftpsClient.setEnabledProtocols(this.protocols);
        }
        if (this.sessionCreation != null) {
            ftpsClient.setEnabledSessionCreation(this.sessionCreation);
        }
        if (this.useClientMode != null) {
            ftpsClient.setUseClientMode(this.useClientMode);
        }
        if (this.sessionCreation != null) {
            ftpsClient.setEnabledSessionCreation(this.sessionCreation);
        }
        if (this.keyManager != null) {
            ftpsClient.setKeyManager(this.keyManager);
        }
        if (this.needClientAuth != null) {
            ftpsClient.setNeedClientAuth(this.needClientAuth);
        }
        if (this.wantsClientAuth != null) {
            ftpsClient.setWantClientAuth(this.wantsClientAuth);
        }
    }
}

From source file:se.natusoft.maven.plugin.ftp.FTPMojo.java

/**
 * Executes the mojo.//from   www  .j a v a2 s .  com
 *
 * @throws MojoExecutionException
 */
public void execute() throws MojoExecutionException {
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(this.targetHost, this.targetPort);
        if (!ftpClient.login(this.userName, this.password)) {
            throw new MojoExecutionException("Failed to login user '" + this.userName + "'!");
        }
        this.getLog()
                .info("FTP: Connected to '" + this.targetHost + ":" + this.targetPort + "':" + this.targetPath);

        ftpClient.setFileTransferMode(FTP.COMPRESSED_TRANSFER_MODE);
        ftpClient.setBufferSize(1024 * 60);

        SourcePaths sourcePaths = new SourcePaths(new File(this.baseDir), this.files);
        this.getLog().info("Files to upload: " + sourcePaths.getSourceFiles().size());

        int fileNo = 0;

        for (File transferFile : sourcePaths.getSourceFiles()) {
            String relPath = this.targetPath
                    + transferFile.getParentFile().getAbsolutePath().substring(this.baseDir.length());

            boolean havePath = ftpClient.changeWorkingDirectory(relPath);
            if (!havePath) {
                if (!mkdir(ftpClient, relPath)) {
                    throw new MojoExecutionException("Failed to create directory '" + relPath + "'!");
                }
                if (!ftpClient.changeWorkingDirectory(relPath)) {
                    throw new MojoExecutionException(
                            "Failed to change to '" + relPath + "' after its been created OK!");
                }
            }

            FileInputStream sendFileStream = new FileInputStream(transferFile);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.storeFile(transferFile.getName(), sendFileStream);
            sendFileStream.close();
            this.getLog().info(
                    "Transferred [" + ++fileNo + "]: " + relPath + File.separator + transferFile.getName());
        }
        this.getLog().info("All files transferred!");
    } catch (IOException ioe) {
        throw new MojoExecutionException(ioe.getMessage(), ioe);
    } finally {
        try {
            ftpClient.disconnect();
        } catch (IOException ioe) {
            this.getLog().error("Failed to disconnect from FTP site! [" + ioe.getMessage() + "]", ioe);
        }
    }
}

From source file:ubic.basecode.util.NetUtils.java

/**
 * Convenient method to get a FTP connection.
 * /*from  www .  ja  va  2 s .com*/
 * @param host
 * @param login
 * @param password
 * @param mode
 * @return
 * @throws SocketException
 * @throws IOException
 */
public static FTPClient connect(int mode, String host, String loginName, String password)
        throws SocketException, IOException {
    FTPClient f = new FTPClient();
    f.enterLocalPassiveMode();
    f.setBufferSize(32 * 2 ^ 20);
    boolean success = false;
    f.connect(host);
    int reply = f.getReplyCode();
    if (FTPReply.isPositiveCompletion(reply))
        success = f.login(loginName, password);
    if (!success) {
        f.disconnect();
        throw new IOException("Couldn't connect to " + host);
    }
    f.setFileType(mode);
    log.debug("Connected to " + host);
    return f;
}

From source file:ubicrypt.core.provider.ftp.FTProvider.java

private Observable<FTPClient> connect() {
    return Observable.<FTPClient>create(subscriber -> {
        final FTPClient client = new FTPClient();
        try {/*w w  w .j  ava 2s. c om*/
            client.connect(conf.getHost(), getConf().getPort() == -1 ? 21 : getConf().getPort());
            final int reply = client.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                log.error("FTP server refused connection:" + client.getReplyString());
                if (client.isConnected()) {
                    client.disconnect();
                }
                subscriber.onError(
                        new RuntimeException("FTP server refused connection:" + client.getReplyString()));
                return;
            }
            if (!getConf().isAnonymous()) {
                if (!client.login(getConf().getUsername(), new String(getConf().getPassword()))) {
                    client.disconnect();
                    log.warn("FTP wrong credentials:" + client.getReplyString());
                    subscriber.onError(new RuntimeException("FTP wrong credentials"));
                }
            }
            client.setFileType(FTP.BINARY_FILE_TYPE);
            client.setBufferSize(1 << 64);
            client.enterLocalPassiveMode();
            client.setControlKeepAliveTimeout(60 * 60); //1h
            if (!isEmpty(conf.getFolder())) {
                final String directory = startsWith("/", conf.getFolder()) ? conf.getFolder()
                        : "/" + conf.getFolder();
                if (!client.changeWorkingDirectory(directory)) {
                    if (!client.makeDirectory(directory)) {
                        disconnect(client);
                        subscriber.onError(new ProviderException(showServerReply(client)));
                        return;
                    }
                    if (!client.changeWorkingDirectory(directory)) {
                        disconnect(client);
                        subscriber.onError(new ProviderException(showServerReply(client)));
                        return;
                    }
                }
            }
            subscriber.onNext(client);
            subscriber.onCompleted();
        } catch (final IOException e) {
            disconnect(client);
            subscriber.onError(e);
        }
    }).subscribeOn(Schedulers.io());
}