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:com.discursive.jccook.net.FTPExample.java

private void secondExample() throws SocketException, IOException {
    FTPClient client = new FTPClient();
    client.connect("ftp.ibiblio.org");
    client.login("anonymous", "");

    String remoteDir = "/pub/mirrors/apache/jakarta/ecs/binaries";

    FTPFile[] remoteFiles = client.listFiles(remoteDir);

    System.out.println("Files in " + remoteDir);

    for (int i = 0; i < remoteFiles.length; i++) {
        String name = remoteFiles[i].getName();
        long length = remoteFiles[i].getSize();
        String readableLength = FileUtils.byteCountToDisplaySize(length);

        System.out.println(name + ":\t\t" + readableLength);
    }//from   www .  java2  s  .  c o m

    client.disconnect();
}

From source file:com.stacksync.desktop.connection.plugins.ftp.FtpTransferManager.java

public FtpTransferManager(FtpConnection connection) {
    super(connection);
    this.ftp = new FTPClient();
}

From source file:com.taurus.compratae.appservice.impl.EnvioArchivoFTPServiceImpl.java

public void conectarFTP(Archivo archivo) {
    FTPClient client = new FTPClient();
    /*String sFTP = "127.0.0.1";
    String sUser = "tae";/*from  ww w .  j  a  v a  2 s .c  o  m*/
    String sPassword = "tae";*/
    String sFTP = buscarParametros(FTP_SERVER);
    String sUser = buscarParametros(FTP_USER);
    String sPassword = buscarParametros(FTP_PASSWORD);
    ///////////////////////////////////
    //String[] lista;
    try {
        client.connect(sFTP);
        boolean login = client.login(sUser, sPassword);
        System.out.println("1. Directorio de trabajo: " + client.printWorkingDirectory());
        client.setFileType(FTP.BINARY_FILE_TYPE);
        BufferedInputStream buffIn = null;
        buffIn = new BufferedInputStream(new FileInputStream(archivo.getNombre()));
        client.enterLocalPassiveMode();
        StringTokenizer tokens = new StringTokenizer(archivo.getNombre(), "/");//Para separar el nombre de la ruta.
        int i = 0;
        String nombre = "";
        while (tokens.hasMoreTokens()) {
            if (i == 1) {
                nombre = tokens.nextToken();
                i++;
            } else {
                i++;
            }
        }
        client.storeFile(nombre, buffIn);
        buffIn.close();
        /*lista = client.listNames();
        for (String lista1 : lista) {
        System.out.println(lista1);
        }*/
        //client.changeWorkingDirectory("\\done");
        //System.out.println("2. Working Directory: " + client.printWorkingDirectory());
        client.logout();
        client.disconnect();
        System.out.println("Termin de conectarme al FTP!!");
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:com.dp2345.plugin.ftp.FtpPlugin.java

@Override
public void upload(String path, File file, String contentType) {
    PluginConfig pluginConfig = getPluginConfig();
    if (pluginConfig != null) {
        String host = pluginConfig.getAttribute("host");
        Integer port = Integer.valueOf(pluginConfig.getAttribute("port"));
        String username = pluginConfig.getAttribute("username");
        String password = pluginConfig.getAttribute("password");
        FTPClient ftpClient = new FTPClient();
        InputStream inputStream = null;
        try {/* w  w w  .ja  v  a 2  s . c  o m*/
            inputStream = new FileInputStream(file);
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                String directory = StringUtils.substringBeforeLast(path, "/");
                String filename = StringUtils.substringAfterLast(path, "/");
                if (!ftpClient.changeWorkingDirectory(directory)) {
                    String[] paths = StringUtils.split(directory, "/");
                    String p = "/";
                    ftpClient.changeWorkingDirectory(p);
                    for (String s : paths) {
                        p += s + "/";
                        if (!ftpClient.changeWorkingDirectory(p)) {
                            ftpClient.makeDirectory(s);
                            ftpClient.changeWorkingDirectory(p);
                        }
                    }
                }
                ftpClient.storeFile(filename, inputStream);
                ftpClient.logout();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(inputStream);
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                }
            }
        }
    }
}

From source file:com.k42b3.kadabra.handler.Ftp.java

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();/*  w  ww.ja v  a2 s  .  c o  m*/
}

From source file:com.busfixer.avlpositionlogger.BusfixerFTP.java

public boolean ftpConnect(String host, String username, String password, int port) {
    try {//from  w w  w. ja  va2s  . c  om
        mFTPClient = new FTPClient();
        // connecting to the host
        mFTPClient.connect(host, port);

        // now check the reply code, if positive mean connection success
        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
            // login using username & password
            boolean status = mFTPClient.login(username, password);

            /*
             * Set File Transfer Mode
             * 
             * To avoid corruption issue you must specified a correct
             * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
             * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE for
             * transferring text, image, and compressed files.
             */
            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
            mFTPClient.enterLocalPassiveMode();

            return status;
        }
    } catch (Exception e) {
        Log.d(TAG, "Error: could not connect to host " + host);
    }

    return false;
}

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

/**
 * ?FTP?./*ww w.  j  av a2 s  . com*/
 * 
 * @param path
 *            FTP??
 * @param filename
 *            FTP???
 * @param input
 *            ?
 * @return ?true?false
 */
public boolean ftpUploadFile(String path, String filename, InputStream input) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    ftp.setControlEncoding("UTF-8");
    try {
        int reply;
        ftp.connect(url, port);
        ftp.login(username, password);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        // 
        ftp.changeWorkingDirectory(path);
        ftp.setBufferSize(1024);
        ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
        // 
        ftp.storeFile(filename, input);
        input.close();
        ftp.logout();
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return success;
}

From source file:com.nononsenseapps.filepicker.sample.ftp.FtpPickerFragment.java

public FtpPickerFragment() {
    super();
    ftp = new FTPClient();
}

From source file:com.legstar.zosjes.FtpZosClient.java

/**
 * No-arg constructor.//from w  ww .  ja v  a  2s.c  o  m
 */
public FtpZosClient() {
    _ftpClient = new FTPClient();
    FTPClientConfig ftpConf = new FTPClientConfig(FTPClientConfig.SYST_MVS);
    ftpConf.setServerTimeZoneId("GMT");
    _ftpClient.configure(ftpConf);
}

From source file:de.aw.awlib.utils.AWRemoteFileServerHandler.java

public AWRemoteFileServerHandler(AWRemoteFileServer remoteFileServer, ExecutionListener executionListener) {
    mRemoteFileServer = remoteFileServer;
    mExecutionListener = executionListener;
    switch (remoteFileServer.getConnectionType()) {
    case SSL:/*  w  w  w.  j av  a2s. com*/
        mClient = new FTPSClient();
        break;
    case NONSSL:
        mClient = new FTPClient();
        break;
    default:
        mClient = null;
    }
}