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

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

Introduction

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

Prototype

public boolean setFileTransferMode(int mode) throws IOException 

Source Link

Document

Sets the transfer mode.

Usage

From source file:fr.acxio.tools.agia.ftp.DefaultFtpClientFactory.java

public FTPClient getFtpClient() throws IOException {
    FTPClient aClient = new FTPClient();

    // Debug output
    // aClient.addProtocolCommandListener(new PrintCommandListener(new
    // PrintWriter(System.out), true));

    if (activeExternalIPAddress != null) {
        aClient.setActiveExternalIPAddress(activeExternalIPAddress);
    }/*from  www . j  a  v a  2 s .co  m*/
    if (activeMinPort != null && activeMaxPort != null) {
        aClient.setActivePortRange(activeMinPort, activeMaxPort);
    }
    if (autodetectUTF8 != null) {
        aClient.setAutodetectUTF8(autodetectUTF8);
    }
    if (bufferSize != null) {
        aClient.setBufferSize(bufferSize);
    }
    if (charset != null) {
        aClient.setCharset(charset);
    }
    if (connectTimeout != null) {
        aClient.setConnectTimeout(connectTimeout);
    }
    if (controlEncoding != null) {
        aClient.setControlEncoding(controlEncoding);
    }
    if (controlKeepAliveReplyTimeout != null) {
        aClient.setControlKeepAliveReplyTimeout(controlKeepAliveReplyTimeout);
    }
    if (controlKeepAliveTimeout != null) {
        aClient.setControlKeepAliveTimeout(controlKeepAliveTimeout);
    }
    if (dataTimeout != null) {
        aClient.setDataTimeout(dataTimeout);
    }
    if (defaultPort != null) {
        aClient.setDefaultPort(defaultPort);
    }
    if (defaultTimeout != null) {
        aClient.setDefaultTimeout(defaultTimeout);
    }
    if (fileStructure != null) {
        aClient.setFileStructure(fileStructure);
    }
    if (keepAlive != null) {
        aClient.setKeepAlive(keepAlive);
    }
    if (listHiddenFiles != null) {
        aClient.setListHiddenFiles(listHiddenFiles);
    }
    if (parserFactory != null) {
        aClient.setParserFactory(parserFactory);
    }
    if (passiveLocalIPAddress != null) {
        aClient.setPassiveLocalIPAddress(passiveLocalIPAddress);
    }
    if (passiveNatWorkaround != null) {
        aClient.setPassiveNatWorkaround(passiveNatWorkaround);
    }
    if (proxy != null) {
        aClient.setProxy(proxy);
    }
    if (receieveDataSocketBufferSize != null) {
        aClient.setReceieveDataSocketBufferSize(receieveDataSocketBufferSize);
    }
    if (receiveBufferSize != null) {
        aClient.setReceiveBufferSize(receiveBufferSize);
    }
    if (remoteVerificationEnabled != null) {
        aClient.setRemoteVerificationEnabled(remoteVerificationEnabled);
    }
    if (reportActiveExternalIPAddress != null) {
        aClient.setReportActiveExternalIPAddress(reportActiveExternalIPAddress);
    }
    if (sendBufferSize != null) {
        aClient.setSendBufferSize(sendBufferSize);
    }
    if (sendDataSocketBufferSize != null) {
        aClient.setSendDataSocketBufferSize(sendDataSocketBufferSize);
    }
    if (strictMultilineParsing != null) {
        aClient.setStrictMultilineParsing(strictMultilineParsing);
    }
    if (tcpNoDelay != null) {
        aClient.setTcpNoDelay(tcpNoDelay);
    }
    if (useEPSVwithIPv4 != null) {
        aClient.setUseEPSVwithIPv4(useEPSVwithIPv4);
    }

    if (systemKey != null) {
        FTPClientConfig aClientConfig = new FTPClientConfig(systemKey);
        if (defaultDateFormat != null) {
            aClientConfig.setDefaultDateFormatStr(defaultDateFormat);
        }
        if (recentDateFormat != null) {
            aClientConfig.setRecentDateFormatStr(recentDateFormat);
        }
        if (serverLanguageCode != null) {
            aClientConfig.setServerLanguageCode(serverLanguageCode);
        }
        if (shortMonthNames != null) {
            aClientConfig.setShortMonthNames(shortMonthNames);
        }
        if (serverTimeZoneId != null) {
            aClientConfig.setServerTimeZoneId(serverTimeZoneId);
        }
        aClient.configure(aClientConfig);
    }

    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("Connecting to : {}", host);
    }

    if (port == null) {
        aClient.connect(host);
    } else {
        aClient.connect(host, port);
    }

    int aReplyCode = aClient.getReplyCode();

    if (!FTPReply.isPositiveCompletion(aReplyCode)) {
        aClient.disconnect();
        throw new IOException("Cannot connect to " + host + ". Returned code : " + aReplyCode);
    }

    try {
        if (localPassiveMode) {
            aClient.enterLocalPassiveMode();
        }

        boolean aIsLoggedIn = false;
        if (account == null) {
            aIsLoggedIn = aClient.login(username, password);
        } else {
            aIsLoggedIn = aClient.login(username, password, account);
        }
        if (!aIsLoggedIn) {
            throw new IOException(aClient.getReplyString());
        }
    } catch (IOException e) {
        aClient.disconnect();
        throw e;
    }

    if (fileTransferMode != null) {
        aClient.setFileTransferMode(fileTransferMode);
    }
    if (fileType != null) {
        aClient.setFileType(fileType);
    }

    return aClient;
}

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

/**
 * Connect to the FTP server using configuration parameters *
 * //from   w  w w. j a v a 2  s .c o m
 * @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:se.natusoft.maven.plugin.ftp.FTPMojo.java

/**
 * Executes the mojo./*from w w w.ja  v a  2  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:tufts.oki.remoteFiling.RemoteByteStore.java

/**
 *  Get the current length of thsi byte store.
 *
 *  @author Mark Norton//from  w w  w  . ja v  a2s  .c  o m
 *
 *  @return The current length of this byte store.
 */
public long length() throws osid.filing.FilingException {
    long length = 0;
    try {
        FTPClient client = rc.getClient();
        client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
        //  The file to open consists of the root base plus, path to current directory plus name.
        //String fn = rc.getRootBase() + ((RemoteCabinet)getParent()).separator() + getDisplayName();
        //String fn = rc.getRootBase() + "/" + getDisplayName();
        String fn = getFullName();
        //System.out.println("length - file name to open: " + fn);
        FTPFile[] replies = client.listFiles(__fileListParser, fn);
        System.out.println("File Name = " + fn + " replies =" + replies + "Client:" + client.getStatus());
        if (replies == null) {
            System.out.println(client.getReplyCode());
            throw new osid.filing.FilingException(
                    "RemoteByteStore.length: " + osid.filing.FilingException.IO_ERROR);
        }
        //System.out.println(client.getReplyCode());
        length = replies[0].getSize();
    } catch (IOException e) {
        throw new osid.filing.FilingException(
                "RemoteByteStore.length: " + osid.filing.FilingException.IO_ERROR);
    }
    return length;
}

From source file:uk.sipperfly.utils.FTPUtil.java

public static void reconnect() throws SocketException, IOException {
    FTPClient ftpClient = new FTPClient();
    ftpClient.setControlEncoding("UTF-8");
    ftpClient.connect(host, port);/*from   w ww  .ja v  a 2  s  .c  o m*/
    ftpClient.login(username, password);
    if (mode.equalsIgnoreCase("passive")) {
        ftpClient.enterLocalPassiveMode();
    } else if (mode.equalsIgnoreCase("active")) {
        ftpClient.enterLocalActiveMode();
    }
    int reply = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        Logger.getLogger(GACOM).log(Level.INFO, "FTP Login: ".concat(ftpClient.getReplyString()));
        ftpClient.disconnect();
    }
    ftpClient.setKeepAlive(true);
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
    ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
    ftpClient.setControlKeepAliveTimeout(300);
    //      ftpClient.sendSiteCommand("RECFM=FB");
    //      ftpClient.sendSiteCommand("LRECL=2000");
    //      ftpClient.sendSiteCommand("BLKSIZE=27000");
    //      ftpClient.sendSiteCommand("CY");
    //      ftpClient.sendSiteCommand("PRI= 50");
    //      ftpClient.sendSiteCommand("SEC=25");

    //      ftpClient.sendSiteCommand("RECFM=FB");
    //      ftpClient.sendSiteCommand("LRECL=2000");
    //      ftpClient.sendSiteCommand("BLOCKSIZE=27000");
    //      ftpClient.sendSiteCommand("SPACE=(CYL,(30,300),RLSE)"); 
    //      ftpClient.sendSiteCommand("TR");
    //      ftpClient.sendSiteCommand("PRI=450");
    //      ftpClient.sendSiteCommand("SEC=4500");

    Logger.getLogger(GACOM).log(Level.INFO, "Reconnected FTP");
    System.out.println("reconnected");
}