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

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

Introduction

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

Prototype

public void connect(InetAddress host, int port) throws SocketException, IOException 

Source Link

Document

Opens a Socket connected to a remote host at the specified port and originating from the current host at a system assigned port.

Usage

From source file:org.schedoscope.export.ftp.FtpExportCSVMRTest.java

private int getFileCount() throws IOException {

    FTPClient ftp = new FTPClient();
    ftp.connect("localhost", 2221);
    ftp.login(EmbeddedFtpSftpServer.FTP_USER_FOR_TESTING, EmbeddedFtpSftpServer.FTP_PASS_FOR_TESTING);
    FTPFile[] files = ftp.listFiles();/*from  w  w  w . j a v  a  2s .  co  m*/

    int fileCounter = 0;
    for (FTPFile f : files) {
        if (f.getName().contains(filePrefix)) {
            fileCounter += 1;
        }
    }
    return fileCounter;
}

From source file:org.shept.util.FtpFileCopy.java

/**
 * @param/*from   ww w . j a v a2 s. c  om*/
 * @return
 *
 * @param config
 * @param ftpC
 * @throws SocketException
 * @throws IOException
 */
private boolean configSetup(FtpConfig config, FTPClient ftpC) throws IOException {
    boolean rc;
    ftpC.connect(config.getHostAddress(), config.getPort());
    rc = ftpC.login(config.getUserName(), config.getPassword());
    if (!rc) {
        logger.error("Ftp could not log to remote server with " + config.getUserName());
        return false;
    }
    if (StringUtils.hasText(config.getServerPath())) {
        int cwdCode = ftpC.cwd(config.getServerPath());
        if (!FTPReply.isPositiveCompletion(cwdCode)) {
            logger.error("Ftp could not change working dir to " + config.getServerPath()
                    + " - Server returned Code " + cwdCode);
            return false;
        }
    }
    rc = ftpC.setFileType(config.getFileType());
    if (!rc) {
        logger.error("Ftp could not change FileType for transmission");
        return false;
    }
    return true;
}

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 w ww  .  j av a2  s  . 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// ww  w . j av  a 2s.co  m
 */
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//w w w .  jav a 2 s  . c  o m
 * @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.tests.connection.plugins.ftp.EmbeddedTestFtpServer.java

public static void mkdir(String path, String user) throws SocketException, IOException {
    FTPClient ftp = new FTPClient();
    ftp.setConnectTimeout(3000);/*from  w w w .j  a  v a2s.c o  m*/
    ftp.setDataTimeout(3000);
    ftp.setDefaultTimeout(3000);

    ftp.connect(HOST, PORT);
    ftp.login(user, PASSWORD1);
    ftp.enterLocalPassiveMode();
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // Important !!!
    ftp.makeDirectory(path);

    ftp.disconnect();
    ftp = null;
}

From source file:org.syncany.tests.plugins.ftp.EmbeddedTestFtpServer.java

public static void createTestFile(String path, String user) throws SocketException, IOException {
    FTPClient ftp = new FTPClient();
    ftp.setConnectTimeout(3000);/*from  ww  w  .  j  a v a  2 s  .  co m*/
    ftp.setDataTimeout(3000);
    ftp.setDefaultTimeout(3000);

    ftp.connect(HOST, PORT);
    ftp.login(user, PASSWORD1);
    ftp.enterLocalPassiveMode();
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // Important !!!
    ftp.storeFile(path, new ByteArrayInputStream(new byte[] { 0x01, 0x02 }));

    ftp.disconnect();
    ftp = null;
}

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

protected FTPClient createClient() throws IOException, ResourceException {

    FTPClient client = createClientInstance();

    beforeConnectProcessing(client);//from  ww  w. java 2s.  com

    client.connect(this.host, this.port);

    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        throw new ResourceException(UTIL.getString("ftp_connect_failed", this.host, this.port)); //$NON-NLS-1$
    }

    if (!client.login(this.username, this.password)) {
        throw new IllegalStateException(UTIL.getString("ftp_login_failed", client.getReplyString())); //$NON-NLS-1$
    }

    afterConnectProcessing(client);

    return client;

}

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

public FTPClient createClient() throws IOException, ResourceException {

    FTPClient client = createClientInstance();

    beforeConnectProcessing(client);/*from  w  w  w.  ja  v  a  2  s. com*/

    client.connect(this.host, this.port);

    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        throw new ResourceException("ftp_connect_failed");
    }

    if (!client.login(this.username, this.password)) {
        throw new IllegalStateException("ftp_login_failed"); //$NON-NLS-1$
    }

    afterConnectProcessing(client);

    return client;

}

From source file:org.ut.biolab.medsavant.shared.util.SeekableFTPStream.java

private FTPClient getFTPClient() throws IOException {
    if (ftpClient == null) {
        FTPClient client = new FTPClient();
        try {//from  ww w .  ja  va  2s .  co  m
            client.connect(host, port);
            int reply = client.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                throw new IOException("Unable to connect to " + host);
            }
            if (!client.login(username, password)) {
                throw new IOException("Unable to login to " + host + " as " + username);
            }
            client.setFileType(FTP.BINARY_FILE_TYPE);
            client.enterLocalPassiveMode();
            client.setSoTimeout(SOCKET_TIMEOUT);
            ftpClient = client;
            client = null;
        } finally {
            if (client != null) {
                client.disconnect();
            }
        }
    }

    return ftpClient;
}