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

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

Introduction

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

Prototype

public FTPClient() 

Source Link

Document

Default FTPClient constructor.

Usage

From source file:co.nubetech.hiho.mapreduce.lib.output.FTPTextOutputFormat.java

@Override
public RecordWriter<K, V> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {

    Configuration conf = job.getConfiguration();

    String ip = conf.get(HIHOConf.FTP_ADDRESS);
    String portno = conf.get(HIHOConf.FTP_PORT);
    String usr = conf.get(HIHOConf.FTP_USER);
    String pwd = conf.get(HIHOConf.FTP_PASSWORD);
    String dir = getOutputPath(job).toString();
    System.out.println("\n\ninside ftpoutputformat" + ip + " " + portno + " " + usr + " " + pwd + " " + dir);
    String keyValueSeparator = conf.get("mapred.textoutputformat.separator", "\t");
    FTPClient f = new FTPClient();
    f.connect(ip, Integer.parseInt(portno));
    f.login(usr, pwd);//from  ww  w . j  av  a 2s .  c o  m
    f.changeWorkingDirectory(dir);
    f.setFileType(FTP.BINARY_FILE_TYPE);

    boolean isCompressed = getCompressOutput(job);
    CompressionCodec codec = null;
    String extension = "";
    if (isCompressed) {
        Class<? extends CompressionCodec> codecClass = getOutputCompressorClass(job, GzipCodec.class);
        codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf);
        extension = codec.getDefaultExtension();
    }
    Path file = getDefaultWorkFile(job, extension);
    FileSystem fs = file.getFileSystem(conf);
    String filename = file.getName();
    if (!isCompressed) {
        // FSDataOutputStream fileOut = fs.create(file, false);
        OutputStream os = f.appendFileStream(filename);
        DataOutputStream fileOut = new DataOutputStream(os);
        return new FTPLineRecordWriter<K, V>(fileOut, new String(keyValueSeparator), f);

    } else {
        // FSDataOutputStream fileOut = fs.create(file, false);
        OutputStream os = f.appendFileStream(filename);
        DataOutputStream fileOut = new DataOutputStream(os);
        return new FTPLineRecordWriter<K, V>(new DataOutputStream(codec.createOutputStream(fileOut)),
                keyValueSeparator, f);
    }
}

From source file:com.eryansky.common.utils.ftp.FtpFactory.java

/**
 * FTP?./*from  w  w w. j ava2 s  . c om*/
 * 
 * @param remotePath
 *            FTP?
 * @param fileName
 *            ???
 * @param localPath
 *            ??
 * @return
 */
public boolean ftpDownFile(String remotePath, String fileName, String localPath) {
    // ?
    boolean success = false;
    // FTPClient
    FTPClient ftp = new FTPClient();
    ftp.setControlEncoding("GBK");
    try {
        int reply;
        // FTP?
        // ??ftp.connect(url)?FTP?
        ftp.connect(url, port);
        // ftp
        ftp.login(username, password);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        // 
        ftp.changeWorkingDirectory(remotePath);
        // 
        FTPFile[] fs = ftp.listFiles();
        // ??
        for (FTPFile ff : fs) {
            if (ff.getName().equals(fileName)) {
                // ???
                File localFile = new File(localPath + "/" + ff.getName());
                // ?
                OutputStream is = new FileOutputStream(localFile);
                // 
                ftp.retrieveFile(ff.getName(), is);
                is.close();
                success = true;
            }
        }
        ftp.logout();
        // ?

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
    return success;
}

From source file:fr.chaffottem.bonita.connector.ftp.FTPClientConnector.java

protected FTPClient build() {
    final Boolean ftps = (Boolean) getInputParameter(FTPS, false);
    if (!ftps) {/*from  w  w  w.  j  a  v  a 2  s . co  m*/
        return new FTPClient();
    } else {
        final String securityProtocol = (String) getInputParameter(SECURITY_PROTOCOL, "TLS");
        final boolean isImplicit = isImplicit();
        return new FTPSClient(securityProtocol, isImplicit);
    }
}

From source file:ftp.FTPtask.java

/**
 * Download File from FTP/*ww w  . j a  va 2  s.  co m*/
  * @param FTPADDR, ?  ?
  * @param user,   
  * @param password,   
 * @param FullPathToPutFile -       ,    ??
 * @param FilenameOnFTP - ?    (?     /upload/)
 */

public static void DownloadFileFromFTP(String FTPADDR, String user, String password, String FullPathToPutFile,
        String FilenameOnFTP) {

    FTPClient client = new FTPClient();
    FileOutputStream fos = null;

    try {
        client.connect(FTPADDR);
        client.login(user, password);

        //       The remote filename to be downloaded.

        String filename = FullPathToPutFile;
        fos = new FileOutputStream(filename);

        //       Download file from FTP server

        client.retrieveFile("/upload/" + FilenameOnFTP, fos);
        //

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:com.claim.support.FtpUtil.java

public void uploadMutiFileWithFTP(String targetDirectory, FileTransferController fileTransferController) {
    try {//from   w  w  w.ja va  2  s.co  m
        FtpProperties properties = new ResourcesProperties().loadFTPProperties();

        FTPClient ftp = new FTPClient();
        ftp.connect(properties.getFtp_server());
        if (!ftp.login(properties.getFtp_username(), properties.getFtp_password())) {
            ftp.logout();
        }
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }
        ftp.enterLocalPassiveMode();
        System.out.println("Remote system is " + ftp.getSystemName());
        ftp.changeWorkingDirectory(targetDirectory);
        System.out.println("Current directory is " + ftp.printWorkingDirectory());
        FTPFile[] ftpFiles = ftp.listFiles();
        if (ftpFiles != null && ftpFiles.length > 0) {
            for (FTPFile file : ftpFiles) {
                if (!file.isFile()) {
                    continue;
                }
                System.out.println("File is " + file.getName());
                OutputStream output;
                output = new FileOutputStream(
                        properties.getFtp_remote_directory() + File.separator + file.getName());
                ftp.retrieveFile(file.getName(), output);
                output.close();
            }
        }
        ftp.logout();
        ftp.disconnect();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.cloudhopper.commons.rfs.provider.FtpRemoteFileSystem.java

public void connect() throws FileSystemException {
    // make sure we don't connect twice
    if (ftp != null) {
        throw new FileSystemException("Already connected to FTP(s) server");
    }/*  ww w .  j a  va2s.  co m*/

    // either create an SSL FTP client or normal one
    if (getProtocol() == Protocol.FTPS) {
        try {
            ftp = new FTPSClient();
        } catch (Exception e) { //} catch (NoSuchAlgorithmException e) {
            throw new FileSystemException("Unable to create FTPS client: " + e.getMessage(), e);
        }
    } else {
        ftp = new FTPClient();
    }

    //
    // connect to ftp(s) server
    //
    try {
        int reply;

        // either connect to the default port or an overridden one
        if (getURL().getPort() == null) {
            ftp.connect(getURL().getHost());
        } else {
            ftp.connect(getURL().getHost(), getURL().getPort().intValue());
        }

        // After connection attempt, check reply code to verify we're connected
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            // make sure we're definitely disconnected before we throw exception
            try {
                ftp.disconnect();
            } catch (Exception e) {
            }
            ftp = null;
            throw new FileSystemException("FTP server refused connection (replyCode=" + reply + ")");
        }

        logger.info("Connected to remote FTP server @ " + getURL().getHost() + " (not authenticated yet)");

    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (Exception ex) {
            }
        }
        ftp = null;
        throw new FileSystemException("Unabled to connect to FTP server @ " + getURL().getHost(), e);
    }

    //
    // login either anonymously or with user/pass combo
    //
    try {
        boolean loggedIn = false;
        if (getURL().getUsername() == null) {
            logger.info("Logging in anonymously to FTP server");
            loggedIn = ftp.login("anonymous", "");
        } else {
            logger.info("Logging in with username and password to FTP server");
            loggedIn = ftp.login(getURL().getUsername(),
                    (getURL().getPassword() == null ? "" : getURL().getPassword()));
        }

        // did the login work?
        if (!loggedIn) {
            throw new FileSystemException("Login failed with FTP server (reply=" + ftp.getReplyString() + ")");
        }

        //
        // if we're using a secure protocol, encrypt the data channel
        //
        if (getProtocol() == Protocol.FTPS) {
            logger.info("Requesting FTP data channel to also be encrypted with SSL/TLS");
            ((FTPSClient) ftp).execPROT("P");
            // ignore if this actually worked or not -- file just fail to copy
        }

        //
        // make sure we're using binary files
        //
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new FileSystemException(
                    "FTP server failed to switch to binary file mode (reply=" + ftp.getReplyString() + ")");
        }

        // should we go into passive or active mode?
        if (mode == Mode.ACTIVE) {
            ftp.enterLocalActiveMode();
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new FileSystemException(
                        "FTP server failed to switch to active mode (reply=" + ftp.getReplyString() + ")");
            }
        } else if (mode == Mode.PASSIVE) {
            ftp.enterLocalPassiveMode();
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new FileSystemException(
                        "FTP server failed to switch to passive mode (reply=" + ftp.getReplyString() + ")");
            }
        }

        //
        // handle making or changing directories
        //
        // if the path is not empty
        if (!StringUtil.isEmpty(getURL().getPath())) {
            // recursively iterate thru directory path and attempt to create the path
            StringTokenizer path = new StringTokenizer(getURL().getPath(), "/");

            // create an array list of tokens
            ArrayList<String> pathParts = new ArrayList<String>();
            while (path.hasMoreTokens()) {
                pathParts.add(path.nextToken());
            }

            // index we'll start searching for
            int i = 0;

            // determine path of directories we're going to take
            if (pathParts.size() > 0 && pathParts.get(i).equals("~")) {
                // stay in home directory once logged in
                // just increment what we'll search from
                i = 1;
            } else {
                // change to root directory first
                if (!ftp.changeWorkingDirectory("/")) {
                    throw new FileSystemException("FTP server failed to change to root directory (reply="
                            + ftp.getReplyString() + ")");
                }
            }

            for (; i < pathParts.size(); i++) {
                // try to change to this directory
                String pathPart = pathParts.get(i);
                boolean changedDir = ftp.changeWorkingDirectory(pathPart);
                if (!changedDir) {
                    if (!mkdir) {
                        // now try to change to it again
                        if (!ftp.changeWorkingDirectory(pathPart)) {
                            throw new FileSystemException("Unable to change to directory " + getURL().getPath()
                                    + " on FTP server: " + pathPart + " does not exist");
                        }
                    } else {
                        // try to create it
                        logger.info("Making new directory on FTP server: " + pathPart);
                        if (!ftp.makeDirectory(pathPart)) {
                            throw new FileSystemException(
                                    "Unable to make directory '" + pathPart + "' on FTP server");
                        }
                        // now try to change to it again
                        if (!ftp.changeWorkingDirectory(pathPart)) {
                            throw new FileSystemException(
                                    "Unable to change to new directory '" + pathPart + "' on FTP server");
                        }
                    }
                }
            }

            // just print out our working directory
        } else {
            // staying in whatever directory we were assigned by default
            // for information purposeds, let's try to print out that dir
            String currentDir = ftp.printWorkingDirectory();
            logger.info("Current FTP working directory: " + currentDir);
        }

    } catch (FileSystemException e) {
        // make sure to disconnect, then rethrow error
        try {
            ftp.disconnect();
        } catch (Exception ex) {
        }
        ftp = null;
        throw e;
    } catch (IOException e) {
        // make sure we're definitely disconnected before we throw exception
        try {
            ftp.disconnect();
        } catch (Exception ex) {
        }
        ftp = null;
        throw new FileSystemException("Underlying IO exception with FTP server during login and setup process",
                e);
    }
}

From source file:com.intellij.diagnostic.SubmitPerformanceReportAction.java

@Nullable
private static String uploadFileToFTP(final File reportPath, @NonNls final String ftpSite,
        @NonNls final String directory, final ProgressIndicator indicator) {
    FTPClient ftp = new FTPClient();
    ftp.setConnectTimeout(30 * 1000);/* w ww  .j  a v a 2 s.com*/
    try {
        indicator.setText("Connecting to server...");
        ftp.connect(ftpSite);
        indicator.setText("Connected to server");

        if (!ftp.login("anonymous", "anonymous@jetbrains.com")) {
            return "Failed to login";
        }
        indicator.setText("Logged in");

        // After connection attempt, you should check the reply code to verify
        // success.
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return "FTP server refused connection: " + reply;
        }
        if (!ftp.changeWorkingDirectory(directory)) {
            return "Failed to change directory";
        }

        // else won't work behind FW
        ftp.enterLocalPassiveMode();

        if (!ftp.setFileType(FTPClient.BINARY_FILE_TYPE)) {
            return "Failed to switch to binary mode";
        }

        indicator.setText("Transferring (" + StringUtil.formatFileSize(reportPath.length()) + ")");
        FileInputStream readStream = new FileInputStream(reportPath);
        try {
            if (!ftp.storeFile(reportPath.getName(), readStream)) {
                return "Failed to upload file";
            }
        } catch (IOException e) {
            return "Error during transfer: " + e.getMessage();
        } finally {
            readStream.close();
        }
        ftp.logout();
        return null;
    } catch (IOException e) {
        return "Failed to upload: " + e.getMessage();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
                // do nothing
            }
        }
    }
}

From source file:dk.netarkivet.common.distribute.IntegrityTestsFTP.java

public void setUp() {
    rs.setUp();//ww w  .j  av  a2s  .  c o  m
    if (!TestInfo.TEMPDIR.exists()) {
        dk.netarkivet.common.utils.TestInfo.TEMPDIR.mkdir();
    }

    FileUtils.removeRecursively(TestInfo.TEMPDIR);
    TestFileUtils.copyDirectoryNonCVS(TestInfo.DATADIR, TestInfo.TEMPDIR);

    /* make 3 duplicates of TestInfo.TESTXML: test1.xml, test2.xml, test3.xml */
    testFile1 = new File(TestInfo.TEMPDIR, "test1.xml");
    testFile2 = new File(TestInfo.TEMPDIR, "test2.xml");
    testFile3 = new File(TestInfo.TEMPDIR, "test3.xml");
    assertTrue("The test xml file must exist", TestInfo.TESTXML.exists());
    FileUtils.copyFile(TestInfo.TESTXML, testFile1);
    FileUtils.copyFile(TestInfo.TESTXML, testFile2);
    FileUtils.copyFile(TestInfo.TESTXML, testFile3);

    /* Read ftp-related settings from settings.xml. */
    final String ftpServerName = Settings.get(CommonSettings.FTP_SERVER_NAME);
    final int ftpServerPort = Integer.parseInt(Settings.get(CommonSettings.FTP_SERVER_PORT));
    final String ftpUserName = Settings.get(CommonSettings.FTP_USER_NAME);
    final String ftpUserPassword = Settings.get(CommonSettings.FTP_USER_PASSWORD);

    /* Connect to test ftp-server. */
    theFTPClient = new FTPClient();

    try {
        theFTPClient.connect(ftpServerName, ftpServerPort);
        assertTrue(
                "Could not login to ' + " + ftpServerName + ":" + ftpServerPort + "' with username,password="
                        + ftpUserName + "," + ftpUserPassword,
                theFTPClient.login(ftpUserName, ftpUserPassword));
        assertTrue("Must be possible to set the file type to binary after login",
                theFTPClient.setFileType(FTPClient.BINARY_FILE_TYPE));
    } catch (SocketException e) {
        throw new IOFailure("Connect to " + ftpServerName + ":" + ftpServerPort + " failed", e.getCause());
    } catch (IOException e) {
        throw new IOFailure("Connect to " + ftpServerName + ":" + ftpServerPort + " failed", e.getCause());
    }

    /** Do not send notification by email. Print them to STDOUT. */
    Settings.set(CommonSettings.NOTIFICATIONS_CLASS, RememberNotifications.class.getName());
}

From source file:dk.netarkivet.common.distribute.IntegrityTestsFTPRemoteFile.java

public void setUp() {
    rs.setUp();//w w w . j  a v  a2 s.co  m
    try {
        if (!TestInfo.TEMPDIR.exists()) {
            dk.netarkivet.common.utils.TestInfo.TEMPDIR.mkdir();
        }

        FileUtils.removeRecursively(TestInfo.TEMPDIR);
        TestFileUtils.copyDirectoryNonCVS(TestInfo.DATADIR, TestInfo.TEMPDIR);

        /* make 3 duplicates of TestInfo.TESTXML: test1.xml, test2.xml, test3.xml */
        testFile1 = new File(TestInfo.TEMPDIR, "test1.xml");
        testFile2 = new File(TestInfo.TEMPDIR, "test2.xml");
        testFile3 = new File(TestInfo.TEMPDIR, "test3.xml");
        assertTrue("The test xml file must exist", TestInfo.TESTXML.exists());
        FileUtils.copyFile(TestInfo.TESTXML, testFile1);
        FileUtils.copyFile(TestInfo.TESTXML, testFile2);
        FileUtils.copyFile(TestInfo.TESTXML, testFile3);
    } catch (Exception e) {
        fail("Could not setup configuration for");
    }

    /** Read ftp-related settings from settings.xml. */
    final String ftpServerName = Settings.get(CommonSettings.FTP_SERVER_NAME);
    final int ftpServerPort = Integer.parseInt(Settings.get(CommonSettings.FTP_SERVER_PORT));
    final String ftpUserName = Settings.get(CommonSettings.FTP_USER_NAME);
    final String ftpUserPassword = Settings.get(CommonSettings.FTP_USER_PASSWORD);

    /** Connect to test ftp-server. */
    theFTPClient = new FTPClient();

    try {
        theFTPClient.connect(ftpServerName, ftpServerPort);
        theFTPClient.login(ftpUserName, ftpUserPassword);
        boolean b = theFTPClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        assertTrue("Must be possible to set the file type to binary after login", b);
    } catch (SocketException e) {
        throw new IOFailure("Connect to " + ftpServerName + " failed", e.getCause());
    } catch (IOException e) {
        throw new IOFailure("Connect to " + ftpServerName + " failed", e.getCause());
    }
    theFTPClient.enterLocalPassiveMode();
    rf = FTPRemoteFile.getInstance(testFile1, true, false, true);

    /** Do not send notification by email. Print them to STDOUT. */
    Settings.set(CommonSettings.NOTIFICATIONS_CLASS, RememberNotifications.class.getName());
}

From source file:com.hibo.bas.fileplugin.file.FtpPlugin.java

@Override
public List<FileInfo> browser(String path) {
    List<FileInfo> fileInfos = new ArrayList<FileInfo>();
    // PluginConfig pluginConfig = getPluginConfig();
    // if (pluginConfig != null) {
    Map<String, String> ftpInfo = getFtpInfo("all");
    String urlPrefix = DataConfig.getConfig("IMGFTPROOT");
    FTPClient ftpClient = new FTPClient();
    try {//from ww w.j a  v  a  2  s  .c  o  m
        ftpClient.connect(ftpInfo.get("host"), 21);
        ftpClient.login(ftpInfo.get("username"), ftpInfo.get("password"));
        ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()) && ftpClient.changeWorkingDirectory(path)) {
            for (FTPFile ftpFile : ftpClient.listFiles()) {
                FileInfo fileInfo = new FileInfo();
                fileInfo.setName(ftpFile.getName());
                fileInfo.setUrl(urlPrefix + path + ftpFile.getName());
                fileInfo.setIsDirectory(ftpFile.isDirectory());
                fileInfo.setSize(ftpFile.getSize());
                fileInfo.setLastModified(ftpFile.getTimestamp().getTime());
                fileInfos.add(fileInfo);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
            }
        }
    }
    // }
    return fileInfos;
}