Package org.apache.commons.net.ftp

Examples of org.apache.commons.net.ftp.FTPClient


            return isDirectory;
        }
    }

    public List<IPath> ls() throws IOException {
        FTPClient client = getClient();
        cwdFtpTo(client, true);
        List<IPath> rezult = new LinkedList<IPath>();
        FTPFile[] names = client.listFiles();
        for (FTPFile i : names) {
            rezult.add(new FileUtilFtpImpl(getPath() + i.getName() + (i.isDirectory() ? File.separatorChar : ""), getUsername(), getPassword(), getHost()));
        }
        client.disconnect();
        return rezult;
    }
View Full Code Here


        return rezult;
    }

    public IPath mkdir(String lastPathName) throws IOException {
        log.debug("FTP mkdir: to=" + this + ", lastPathName=" + lastPathName);
        FTPClient client = getClient();
        cwdFtpTo(client, true);
        if (!client.makeDirectory(lastPathName) && client.getReplyCode() != 550) {
            String message = "error during mkdir ,code : " + client.getReplyCode() + ", reply: " + client.getReplyString();
            log.warn(message);
        } else {
            log.debug("mkdir responce code : " + client.getReplyCode() + ", reply: " + client.getReplyString());
        }
        return new FileUtilFtpImpl(getPath() + lastPathName + File.separatorChar, getUsername(), getPassword(), getHost());
    }
View Full Code Here

    @Override
    public boolean delete() throws IOException {
        log.debug("ftp delete file " + getPath());
        boolean res = false;
        FTPClient client = getClient();
        if (isDirectory()) {
            for (IPath i : ls()) {
                res |= !i.delete();
            }
            res |= client.deleteFile(getPath());
            log.debug("ftp delete file " + getPath() + " reply code: " + client.getReplyCode() + ", reply string: " + client.getReplyString());
            client.disconnect();
            return !res;
        } else {
            try {
                return client.deleteFile(getPath());
            } finally {
                log.debug("ftp delete file " + getPath() + " reply code: " + client.getReplyCode() + ", reply string: " + client.getReplyString());
                client.disconnect();
            }
        }

    }
View Full Code Here

  public Ftp(Resource resource, String basePath) throws Exception
  {
    super(resource, basePath);

    client = new FTPClient();
    client.addProtocolCommandListener(new CommandLogger());
    client.connect(resource.getString("host"), Integer.parseInt(resource.getString("port")));
    client.login(resource.getString("user"), resource.getString("pw"));
    client.setFileType(FTPClient.BINARY_FILE_TYPE);
    client.enterLocalPassiveMode();
View Full Code Here

        }

        protected FTPClient instantiateClient()
            throws RemoteFileSystemException
        {
                return new FTPClient() ;
        }
View Full Code Here

                                     + ftp.getReplyString());
        }
    }

    public void doFTP() throws BuildException {
        FTPClient ftp = null;

        try {
            task.log("Opening FTP connection to " + task.getServer(), Project.MSG_VERBOSE);

            ftp = new FTPClient();
            if (task.isConfigurationSet()) {
                ftp = FTPConfigurator.configure(ftp, task);
            }

            ftp.setRemoteVerificationEnabled(task.getEnableRemoteVerification());
            ftp.connect(task.getServer(), task.getPort());
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("FTP connection failed: "
                                         + ftp.getReplyString());
            }

            task.log("connected", Project.MSG_VERBOSE);
            task.log("logging in to FTP server", Project.MSG_VERBOSE);

            if ((task.getAccount() != null && !ftp.login(task.getUserid(), task.getPassword(), task.getAccount()))
                || (task.getAccount() == null && !ftp.login(task.getUserid(), task.getPassword()))) {
                throw new BuildException("Could not login to FTP server");
            }

            task.log("login succeeded", Project.MSG_VERBOSE);

            if (task.isBinary()) {
                ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new BuildException("could not set transfer type: "
                                             + ftp.getReplyString());
                }
            } else {
                ftp.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new BuildException("could not set transfer type: "
                                             + ftp.getReplyString());
                }
            }

            if (task.isPassive()) {
                task.log("entering passive mode", Project.MSG_VERBOSE);
                ftp.enterLocalPassiveMode();
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new BuildException("could not enter into passive "
                                             + "mode: " + ftp.getReplyString());
                }
            }

            // If an initial command was configured then send it.
            // Some FTP servers offer different modes of operation,
            // E.G. switching between a UNIX file system mode and
            // a legacy file system.
            if (task.getInitialSiteCommand() != null) {
                RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task);
                final FTPClient lftp = ftp;
                executeRetryable(h, new Retryable() {
                        public void execute() throws IOException {
                            doSiteCommand(lftp, task.getInitialSiteCommand());
                        }
                    }, "initial site command: " + task.getInitialSiteCommand());
            }


            // For a unix ftp server you can set the default mask for all files
            // created.

            if (task.getUmask() != null) {
                RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task);
                final FTPClient lftp = ftp;
                executeRetryable(h, new Retryable() {
                        public void execute() throws IOException {
                            doSiteCommand(lftp, "umask " + task.getUmask());
                        }
                    }, "umask " + task.getUmask());
            }

            // If the action is MK_DIR, then the specified remote
            // directory is the directory to create.

            if (task.getAction() == FTPTask.MK_DIR) {
                RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task);
                final FTPClient lftp = ftp;
                executeRetryable(h, new Retryable() {
                        public void execute() throws IOException {
                            makeRemoteDir(lftp, task.getRemotedir());
                        }
                    }, task.getRemotedir());
            } else if (task.getAction() == FTPTask.SITE_CMD) {
                RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task);
                final FTPClient lftp = ftp;
                executeRetryable(h, new Retryable() {
                        public void execute() throws IOException {
                            doSiteCommand(lftp, task.getSiteCommand());
                        }
                    }, "Site Command: " + task.getSiteCommand());
View Full Code Here

     * public void testLoginWithMaxConnectionsMulti() throws Exception { for(int
     * i = 0; i<50; i++) { testLoginWithMaxConnections(); } }
     */

    public void testLoginWithMaxConnections() throws Exception {
        FTPClient client1 = new FTPClient();
        FTPClient client2 = new FTPClient();
        FTPClient client3 = new FTPClient();
        FTPClient client4 = new FTPClient();

        try {
            client1.connect("localhost", port);
            client2.connect("localhost", port);
            client3.connect("localhost", port);
            client4.connect("localhost", port);

            assertTrue(client1.login(TESTUSER1_USERNAME, TESTUSER_PASSWORD));
            assertTrue(client2.login(TESTUSER1_USERNAME, TESTUSER_PASSWORD));
            assertTrue(client3.login(TESTUSER1_USERNAME, TESTUSER_PASSWORD));

            try {
                assertTrue(client4.login(TESTUSER1_USERNAME, TESTUSER_PASSWORD));
                assertEquals(421, client.getReplyCode());
                fail("Must throw FTPConnectionClosedException");
            } catch (FTPConnectionClosedException e) {
                // expected
            }
View Full Code Here

*
*/
public class BindExceptionSerialTest extends ClientTestTemplate {
    @Override
    protected FTPClient createFTPClient() throws Exception {
        FTPClient c = super.createFTPClient();
        c.setDataTimeout(1000);
        return c;
    }
View Full Code Here

        // default cstr
    }
   
    @Override
    protected FTPClient createFTPClient() throws Exception {
        FTPClient c = super.createFTPClient();
        c.setDataTimeout(1000);
        return c;
    }
View Full Code Here

    private String password;
    private boolean binaryMode = true;
    private FTPClientConfig config;

    public boolean validateObject(Object object) {
        FTPClient client = (FTPClient) object;
        try {
            return client.sendNoOp();
        }
        catch (IOException e) {
            throw new RuntimeException("Failed to validate client: " + e, e);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.net.ftp.FTPClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.