Package org.apache.commons.net.ftp

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


        }
        InputStream input = null;
        OutputStream output = null;

        res.sampleStart();
        FTPClient ftp = new FTPClient();
        try {
            savedClient = ftp;
            final int port = getPortAsInt();
            if (port > 0){
                ftp.connect(getServer(),port);               
            } else {
                ftp.connect(getServer());
            }
            res.latencyEnd();
            int reply = ftp.getReplyCode();
            if (FTPReply.isPositiveCompletion(reply))
            {
                if (ftp.login( getUsername(), getPassword())){
                    if (binaryTransfer) {
                        ftp.setFileType(FTP.BINARY_FILE_TYPE);
                    }
                    ftp.enterLocalPassiveMode();// should probably come from the setup dialog
                    boolean ftpOK=false;
                    if (isUpload()) {
                        String contents=getLocalFileContents();
                        if (contents.length() > 0){
                            byte bytes[] = contents.getBytes();// TODO this assumes local encoding
                            input = new ByteArrayInputStream(bytes);
                            res.setBytes(bytes.length);
                        } else {
                            File infile = new File(local);
                            res.setBytes((int)infile.length());
                            input = new FileInputStream(infile);
                        }
                        ftpOK = ftp.storeFile(remote, input);
                    } else {
                        final boolean saveResponse = isSaveResponse();
                        ByteArrayOutputStream baos=null; // No need to close this
                        OutputStream target=null; // No need to close this
                        if (saveResponse){
                            baos  = new ByteArrayOutputStream();
                            target=baos;
                        }
                        if (local.length()>0){
                            output=new FileOutputStream(local);
                            if (target==null) {
                                target=output;
                            } else {
                                target = new TeeOutputStream(output,baos);
                            }
                        }
                        if (target == null){
                            target=new NullOutputStream();
                        }
                        input = ftp.retrieveFileStream(remote);
                        if (input == null){// Could not access file or other error
                            res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                            res.setResponseMessage(ftp.getReplyString());
                        } else {
                            long bytes = IOUtils.copy(input,target);
                            ftpOK = bytes > 0;
                            if (saveResponse && baos != null){
                                res.setResponseData(baos.toByteArray());
                                if (!binaryTransfer) {
                                    res.setDataType(SampleResult.TEXT);
                                }
                            } else {
                                res.setBytes((int) bytes);
                            }
                        }
                    }

                    if (ftpOK) {
                        res.setResponseCodeOK();
                        res.setResponseMessageOK();
                        res.setSuccessful(true);
                    } else {
                        res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                        res.setResponseMessage(ftp.getReplyString());
                    }
                } else {
                    res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                    res.setResponseMessage(ftp.getReplyString());
                }
            } else {
                res.setResponseCode("501"); // TODO
                res.setResponseMessage("Could not connect");
                //res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                res.setResponseMessage(ftp.getReplyString());
            }
        } catch (IOException ex) {
            res.setResponseCode("000"); // TODO
            res.setResponseMessage(ex.toString());
        } finally {
            savedClient = null;
            if (ftp.isConnected()) {
                try {
                    ftp.logout();
                } catch (IOException ignored) {
                }
                try {
                    ftp.disconnect();
                } catch (IOException ignored) {
                }
            }
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(output);
View Full Code Here


        return res;
    }

    /** {@inheritDoc} */
    public boolean interrupt() {
        FTPClient client = savedClient;
        if (client != null) {
            savedClient = null;
            try {
                client.abort();
                client.disconnect();
            } catch (IOException ignored) {
            }
        }
        return client != null;
    }
View Full Code Here

    /**
     * @see java.net.URLConnection#getInputStream()
     */
    public InputStream getInputStream() throws IOException {
        FTPClient client = new FTPClient();
        client.connect(host);
        String replyString = client.getReplyString();
        int replyCode = client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            client.disconnect();
            throw new IOException(replyString);
        }
        if (!client.login(username, password)) {
            replyString = client.getReplyString();
            client.logout();
            throw new IOException(replyString);
        }
        client.setFileType(FTP.BINARY_FILE_TYPE);
        client.enterLocalPassiveMode();

        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            client.retrieveFile(path, os);
            client.logout();
        } finally {
            client.disconnect();
        }
        return new ByteArrayInputStream(os.toByteArray());
    }
View Full Code Here

        this.user = (String) workItem.getParameter("User");
        this.password = (String) workItem.getParameter("Password");
        this.filePath = (String) workItem.getParameter("FilePath");
       
        client = new FTPClient();
        try {
            if(connection != null){
                client.connect(connection.getHost(), Integer.parseInt(connection.getPort()));
                int reply = client.getReplyCode();
View Full Code Here

        cryptoUtil.process(c);
        LOG.info("*****load from server done******");
        assertTrue(FileUtils.contentEquals(new File(from.getPath()), new File(response.getPath())));
        LOG.info("*****assert files done******");
        new File(prefix + "2.pdf").delete();
        FTPClient client = new FTPClient();
        client.connect(readConfig.getUrl());
        client.login(readConfig.getLogin(), readConfig.getPass());
        client.deleteFile("~/backup/1/1.pdf");
        client.disconnect();
        LOG.info("*****clean directories done******\n");
    }
View Full Code Here

    }

    private static Log log = LogFactory.getLog(FileUtilFtpImpl.class);

    public byte[] read() throws IOException {
        FTPClient client = getClient();
        client.setFileType(FTPClient.BINARY_FILE_TYPE);
        String[] pathes = this.getPath().replaceAll("\\\\", "/").split("/");
        String filename = pathes[pathes.length - 1];
        cwdFtpTo(client, true);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        client.retrieveFile(filename, outputStream);
        outputStream.close();
        byte buffer[] = outputStream.toByteArray();
        client.disconnect();
        log.debug("read ftp done");
        return buffer;
    }
View Full Code Here

        log.debug("read ftp done");
        return buffer;
    }

    protected FTPClient getClient() throws IOException {
        FTPClient client = new FTPClient();
        log.debug("connect to host " + getHost());
        client.connect(getHost());
        log.debug("login using username " + getUsername());
        if (!client.login(getUsername(), getPassword())){
            throw new IOException("user is not login, code " + client.getReplyCode() + ", message: " + client.getReplyString());
        }
        return client;
    }
View Full Code Here

        return client;
    }

    public void write(String filename, byte[] data) throws IOException {
        log.debug("FTP WRITE: to = " + this + ", filename = " + filename + ", data.length = " + data.length);
        FTPClient client = getClient();
        client.setFileType(FTPClient.BINARY_FILE_TYPE);
        cwdFtpTo(client, true);
        if (filename.isEmpty()) {
            filename = getPath().substring(getPath().lastIndexOf('/') + 1);
            log.debug("FTP WRITE: new filename = '" + filename + "'");
        }
        log.debug("rescheck if file exist");
        int size = 0;
        FTPFile[] files = client.listFiles();
        for (FTPFile i : files) {
            if (i.getName().equals(filename)) {
                log.debug("File " + i.getName() + " exist, appending");
                if (i.getSize() > Integer.MAX_VALUE) {
                    log.error("Sorry, we not support files bigger than " + Integer.MAX_VALUE + " bytes");
                    throw new RuntimeException("Sorry, we not support files bigger than " + Integer.MAX_VALUE + " bytes");
                }
                size = (int) i.getSize();
            }
        }
        log.debug("FTP WRITE STARTED");
        long start = System.currentTimeMillis();
        log.trace("clientappendfile=" + client.appendFile(filename, new ByteArrayInputStream(data, size, data.length - size)));
        log.trace("client responce code:" + client.getReplyCode() + ", string:" + client.getReplyString());
        long end = System.currentTimeMillis();
        log.debug("FTP WRITE FINISHED, time = " + (end - start) + ", speed: " + ((data.length * 1000) / (128 * (end - start))) + "kbps");
        if (client.getReplyCode() != 226) {
            log.error("client error code:" + client.getReplyCode() + ", string:" + client.getReplyString());
            throw new IOException(client.getReplyString());
        }
        client.disconnect();
        log.trace("save to ftp done");
    }
View Full Code Here

        return true;
    }

    public boolean exists() throws IOException {
        log.debug("exist  : " + this);
        FTPClient client = getClient();
        String[] pathes = getPath().replaceAll("\\\\", "/").split("/");
        boolean find = false;
        if (pathes.length > 0) {
            for (int i = 0; i < pathes.length - 1; i++){
                log.trace("Ftp client: we at: " + client.printWorkingDirectory() + ", go to \'" + pathes[i] + "'");
                int reply = client.cwd(pathes[i]);
                if (reply >= 400) {
                    log.warn("exist cwd " + pathes[i] + " responce code : " + client.getReplyCode() + ", reply: " + client.getReplyString());
                    return false;
                }
                log.trace("exist responce code : " + client.getReplyCode() + ", reply: " + client.getReplyString());
               
            }
            FTPFile files[] = client.listFiles();
            for (FTPFile f : files) {
                if (f != null) {
                    find |= pathes[pathes.length - 1].equals(f.getName());
                } else {
                    log.warn("exist: listfiles() return null");
View Full Code Here

    public boolean isDirectory() throws IOException {
        String normalizedTo = getPath().replaceAll("\\\\", "/");
        boolean isDirectory = normalizedTo.charAt(normalizedTo.length() - 1) == '/';
        if (isDirectory == true)
            return true;
        FTPClient client = getClient();
        isDirectory = cwdFtpTo(client, false);
        if (isDirectory == false) {
            client.disconnect();
            return isDirectory;
        } else {
            isDirectory = client.changeWorkingDirectory(normalizedTo.split("/")[normalizedTo.split("/").length - 1]);
            client.disconnect();
            return isDirectory;
        }
    }
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.