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:org.sofun.platform.opta.ftp.FTPClientWrapper.java

/**
 * Establishes a connection w/ the FTP server.
 * /* w  ww. j  av a2 s.com*/
 * @throws OptaException
 */
public void connect() throws OptaException {
    if (client == null || !client.isAvailable()) {
        client = new FTPClient();
    }
    try {
        client.connect(host, port);
        int reply = client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            client.disconnect();
            throw new OptaException("FTP server refused connection.");
        }
    } catch (SocketException e) {
        throw new OptaException(e.getMessage());
    } catch (IOException e) {
        if (client.isConnected()) {
            try {
                client.disconnect();
            } catch (IOException f) {
                // do nothing: just spit stack trace.
                f.printStackTrace();
            }
        }
        throw new OptaException(e.getMessage());
    }
    try {
        if (!client.login(username, password)) {
            client.logout();
            throw new OptaException("Cannot login...");
        }
        client.enterLocalPassiveMode();
    } catch (Exception e) {
        throw new OptaException(e.getMessage());
    }
}

From source file:org.spionen.james.FTPConnection.java

public FTPConnection(String hostname, String username, String password, String channel) {
    this.hostname = hostname;
    this.username = username;
    this.password = password;
    this.channel = channel;
    client = new FTPClient();
}

From source file:org.spongepowered.repoindexer.FTPUtils.java

public static boolean uploadFTP(String user, String pass, String server, File local, String remoteLocation) {
    FTPClient ftpClient = new FTPClient();
    boolean done = false;
    try {// w  w w  .  j  av a  2 s  .  com

        ftpClient.connect(server);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        InputStream inputStream = new FileInputStream(local);
        done = ftpClient.storeFile(remoteLocation, inputStream);
        inputStream.close();
    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
        return false;
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return done;
    }
}

From source file:org.springframework.integration.ftp.DefaultFtpClientFactory.java

@Override
protected FTPClient createSingleInstanceOfClient() {
    return new FTPClient();
}

From source file:org.springframework.integration.ftp.session.DefaultFtpSessionFactory.java

@Override
protected FTPClient createClientInstance() {
    return new FTPClient();
}

From source file:org.structr.files.ftp.FtpAccessTest.java

public void test01LoginFailed() {

    try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {

        FTPClient ftp = new FTPClient();

        ftp.connect("127.0.0.1", ftpPort);
        logger.log(Level.INFO, "Reply from FTP server:", ftp.getReplyString());

        int reply = ftp.getReplyCode();
        assertTrue(FTPReply.isPositiveCompletion(reply));

        boolean loginSuccess = ftp.login("jt978hasdl", "lj3498ha");
        logger.log(Level.INFO, "Try to login as jt978hasdl/lj3498ha:", loginSuccess);
        assertFalse(loginSuccess);//from   ww w  .  j  a  v a2s.c  o  m

        ftp.disconnect();

    } catch (IOException | FrameworkException ex) {
        logger.log(Level.SEVERE, "Error in FTP test", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

}

From source file:org.structr.web.common.FtpTest.java

/**
 * Creates an FTP client, a backend user and logs this user in.
 *
 * @return/*  www . ja  va  2 s . c om*/
 */
protected FTPClient setupFTPClient() {

    FTPClient ftp = new FTPClient();

    try {

        ftp.connect("localhost", ftpPort);
        logger.log(Level.INFO, "Reply from FTP server:", ftp.getReplyString());

        int reply = ftp.getReplyCode();
        assertTrue(FTPReply.isPositiveCompletion(reply));

        String username = "ftpuser1";
        String password = "ftpuserpw1";

        try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {
            ftpUser = createFTPUser(username, password);
            tx.success();
        } catch (FrameworkException fex) {
            logger.log(Level.SEVERE, "Unable to create FTP user", fex);
        }
        boolean loginSuccess = ftp.login(username, password);
        logger.log(Level.INFO, "Try to login as " + username + "/" + password + ": ", loginSuccess);
        assertTrue(loginSuccess);

        reply = ftp.getReplyCode();
        assertEquals(FTPReply.USER_LOGGED_IN, reply);

    } catch (IOException ex) {
        logger.log(Level.SEVERE, "Error in FTP test", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

    return ftp;

}

From source file:org.structr.web.files.FtpTest.java

/**
 * Creates an FTP client, a backend user and logs this user in.
 *
 * @param username//from ww  w . ja  va2s  .  c om
 * @return
 */
protected FTPClient setupFTPClient(final String username) {

    FTPClient ftp = new FTPClient();

    try {

        ftp.connect("localhost", ftpPort);
        logger.info("Reply from FTP server:", ftp.getReplyString());

        int reply = ftp.getReplyCode();
        assertTrue(FTPReply.isPositiveCompletion(reply));

        String password = "ftpuserpw1";

        try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {
            ftpUser = createFTPUser(username, password);
            tx.success();
        } catch (FrameworkException fex) {
            logger.error("Unable to create FTP user", fex);
        }

        boolean loginSuccess = ftp.login(username, password);
        logger.info("Tried to login as {}/{}: {}", new Object[] { username, password, loginSuccess });
        assertTrue(loginSuccess);

        reply = ftp.getReplyCode();
        assertEquals(FTPReply.USER_LOGGED_IN, reply);

    } catch (IOException ex) {
        logger.error("Error in FTP test", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

    return ftp;

}

From source file:org.syncany.connection.plugins.ftp.FtpTransferManager.java

public FtpTransferManager(FtpConnection connection) {
    super(connection);

    this.ftp = new FTPClient();
    this.ftpIsLoggedIn = false;

    this.repoPath = connection.getPath().startsWith("/") ? connection.getPath() : "/" + connection.getPath();
    this.multichunkPath = repoPath + "/multichunks";
    this.databasePath = repoPath + "/databases";
}

From source file:org.syncany.plugins.ftp.FtpTransferManager.java

public FtpTransferManager(FtpTransferSettings connection, Config config) {
    super(connection, config);

    this.ftp = new FTPClient();
    this.ftpIsLoggedIn = false;

    this.repoPath = connection.getPath().startsWith("/") ? connection.getPath() : "/" + connection.getPath();
    this.multichunksPath = repoPath + "/multichunks";
    this.databasesPath = repoPath + "/databases";
    this.actionsPath = repoPath + "/actions";
    this.transactionsPath = repoPath + "/transactions";
    this.temporaryPath = repoPath + "/temporary";
}