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

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

Introduction

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

Prototype

@Override
public void disconnect() throws IOException 

Source Link

Document

Closes the connection to the FTP server and restores connection parameters to the default values.

Usage

From source file:Main.java

public static void main(String[] args) {
    FTPClient client = new FTPClient();
    FileOutputStream fos = null;/*from   ww w .  j av  a  2 s  . co m*/

    client.connect("ftp.domain.com");
    client.login("admin", "secret");

    String filename = "sitemap.xml";
    fos = new FileOutputStream(filename);

    client.retrieveFile("/" + filename, fos);
    fos.close();
    client.disconnect();
}

From source file:Main.java

public static void main(String[] args) {
    FTPClient client = new FTPClient();

    client.connect("ftp.domain.com");
    client.login("admin", "secret");
    String filename = "/testing/data.txt";
    boolean deleted = client.deleteFile(filename);
    if (deleted) {
        System.out.println("File deleted...");
    }//  w w w .ja va  2s. c om

    client.logout();
    client.disconnect();
}

From source file:FtpConnectDemo.java

public static void main(String[] args) {
    FTPClient client = new FTPClient();

    client.connect("ftp.domain.com");
    boolean login = client.login("admin", "secret");

    if (login) {//w ww . jav  a 2  s .  c  om
        System.out.println("Login success...");
        boolean logout = client.logout();
        if (logout) {
            System.out.println("Logout from FTP server...");
        }
    } else {
        System.out.println("Login fail...");
    }
    client.disconnect();
}

From source file:dataflow.examples.TransferFiles.java

public static void main(String[] args) throws IOException, URISyntaxException {
    Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);
    List<FilePair> filePairs = new ArrayList<FilePair>();

    URI ftpInput = new URI(options.getInput());

    FTPClient ftp = new FTPClient();
    ftp.connect(ftpInput.getHost());//from ww  w  .  j a v  a  2  s  . com

    // After connection attempt, you should check the reply code to verify
    // success.
    int reply = ftp.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        logger.error("FTP server refused connection.");
        throw new RuntimeException("FTP server refused connection.");
    }

    ftp.login("anonymous", "someemail@gmail.com");

    String ftpPath = ftpInput.getPath();
    FTPFile[] files = ftp.listFiles(ftpPath);

    URI gcsUri = null;
    if (options.getOutput().endsWith("/")) {
        gcsUri = new URI(options.getOutput());
    } else {
        gcsUri = new URI(options.getOutput() + "/");
    }

    for (FTPFile f : files) {
        logger.info("File: " + f.getName());
        FilePair p = new FilePair();
        p.server = ftpInput.getHost();
        p.ftpPath = f.getName();

        // URI ftpURI = new URI("ftp", p.server, f.getName(), "");
        p.gcsPath = gcsUri.resolve(FilenameUtils.getName(f.getName())).toString();

        filePairs.add(p);
    }

    ftp.logout();

    Pipeline p = Pipeline.create(options);
    PCollection<FilePair> inputs = p.apply(Create.of(filePairs));
    inputs.apply(ParDo.of(new FTPToGCS()).named("CopyToGCS"))
            .apply(AvroIO.Write.withSchema(FilePair.class).to(options.getOutput()));
    p.run();
}

From source file:FTPConnect.FTPConnect.java

public static void main(String[] args) throws IOException {
    FTPClient ftpClient = new FTPClient();
    boolean result;
    try {//from   w w w  .  ja  va  2 s  .c om
        // Connect to the localhost
        ftpClient.connect("localhost");

        // login to ftp server
        result = ftpClient.login("admin", "password");
        if (result == true) {
            System.out.println("Successfully logged in!");
        } else {
            System.out.println("Login Fail!");
            return;
        }
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } finally {
        try {
            ftpClient.disconnect();
        } catch (FTPConnectionClosedException e) {
            System.out.println(e);
        }
    }

}

From source file:Main.java

public static void main(String[] args) {
    FTPClient client = new FTPClient();

    client.connect("ftp.domain.com");
    client.login("admin", "secret");

    String[] names = client.listNames();
    for (String name : names) {
        System.out.println("Name = " + name);
    }//from   w w  w  . j a  v a2 s. c o  m

    FTPFile[] ftpFiles = client.listFiles();
    for (FTPFile ftpFile : ftpFiles) {
        // Check if FTPFile is a regular file
        if (ftpFile.getType() == FTPFile.FILE_TYPE) {
            System.out.println("FTPFile: " + ftpFile.getName() + "; "
                    + FileUtils.byteCountToDisplaySize(ftpFile.getSize()));
        }
    }
    client.logout();
    client.disconnect();
}

From source file:ftp_server.FileUploadDemo.java

public static void main(String[] args) {
    FTPClient client = new FTPClient();
    FileInputStream fis = null;//from   www .  j av  a  2s  . com
    try {
        client.connect("shamalmahesh.net78.net");
        client.login("a9959679", "9P3IckDo");
        //
        // Create an InputStream of the file to be uploaded
        //
        String filename = "hello.txt";
        fis = new FileInputStream(filename);
        //
        // Store file to server
        //
        client.storeFile(filename, fis);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.run.FtpClientExample.java

public static void main(String[] args) {

    FTPClient client = new FTPClient();
    FileOutputStream fos = null;//  w  w  w.j  a  va2s  . com

    try {
        client.connect(ARDroneConstants.IP_ADDRESS, ARDroneConstants.FTP_PORT);

        if (!client.login("anonymous", ""))
            System.err.println("login failed");

        client.setFileType(FTP.BINARY_FILE_TYPE);

        String filename = "version.txt";
        fos = new FileOutputStream(filename);

        if (!client.retrieveFile("/" + filename, fos))
            System.err.println("cannot find file");

        System.out.println("done");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.flush();
                fos.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:luisjosediez.Ejercicio2.java

/**
 * @param args the command line arguments
 *//*from  w  w w  .  ja  va 2  s. c  om*/
public static void main(String[] args) {
    // TODO code application logic here
    System.out.println("Introduce la direccin de un servidor ftp: ");
    FTPClient cliente = new FTPClient();
    String servFTP = cadena();
    String clave = "";
    System.out.println("Introduce usuario (vaco para conexin annima): ");
    String usuario = cadena();
    String opcion;
    if (usuario.equals("")) {
        clave = "";
    } else {
        System.out.println("Introduce contrasea: ");
        clave = cadena();
    }
    try {
        cliente.setPassiveNatWorkaround(false);
        cliente.connect(servFTP, 21);
        boolean login = cliente.login(usuario, clave);
        if (login) {
            System.out.println("Conexin ok");
        } else {
            System.out.println("Login incorrecto");
            cliente.disconnect();
            System.exit(1);
        }
        do {

            System.out.println("Orden [exit para salir]: ");
            opcion = cadena();
            if (opcion.equals("ls")) {
                FTPFile[] files = cliente.listFiles();
                String tipos[] = { "Fichero", "Directorio", "Enlace" };
                for (int i = 0; i < files.length; i++) {
                    System.out.println("\t" + files[i].getName() + "\t=> " + tipos[files[i].getType()]);
                }
            } else if (opcion.startsWith("cd ")) {
                try {
                    cliente.changeWorkingDirectory(opcion.substring(3));
                } catch (IOException e) {
                }
            } else if (opcion.equals("help")) {
                System.out.println(
                        "Puede ejecutar los comandos 'exit', 'ls', 'cd', 'get' y 'upload'. Para ms detalles utilice 'help <comando>'.");
            } else if (opcion.startsWith("help ")) {
                if (opcion.endsWith(" get")) {
                    System.out.println(
                            "Permite descargar un archivo concreto. Uso: 'get <rutaArchivoADescargar>'.");
                } else if (opcion.endsWith(" ls")) {
                    System.out.println("Lista los ficheros y directorios en la ubicacin actual. Uso: 'ls'.");
                } else if (opcion.endsWith(" cd")) {
                    System.out.println("Permite cambiar la ubicacin actual. Uso: 'cd <rutaDestino>'.");
                } else if (opcion.endsWith(" put")) {
                    System.out.println(
                            "Permite subir un archivo al directorio actual. Uso: 'put <rutaArchivoASubir>'.");
                }
            } else if (opcion.startsWith("get ")) {
                try {
                    System.out.println("Indique la carpeta de descarga: ");
                    try (FileOutputStream fos = new FileOutputStream(cadena() + opcion.substring(4))) {
                        cliente.retrieveFile(opcion.substring(4), fos);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                }
            } else if (opcion.startsWith("put ")) {
                try {
                    try {

                        System.out.println(opcion.substring(4));

                        File local = new File(opcion.substring(4));

                        System.out.println(local.getName());

                        InputStream is = new FileInputStream(opcion.substring(4));

                        OutputStream os = cliente.storeFileStream(local.getName());

                        byte[] bytesIn = new byte[4096];

                        int read = 0;

                        while ((read = is.read(bytesIn)) != -1) {

                            os.write(bytesIn, 0, read);

                        }

                        is.close();
                        os.close();

                        boolean completed = cliente.completePendingCommand();

                        if (completed) {
                            System.out.println("The file is uploaded successfully.");
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                }
            }

        } while (!(opcion.equals("exit")));

        boolean logout = cliente.logout();
        if (logout)
            System.out.println("Logout...");
        else
            System.out.println("Logout incorrecto");

        cliente.disconnect();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:FTPConnect.FTPBajarArchivo.java

public static void main(String[] args) throws IOException {
    FTPClient ftpClient = new FTPClient();
    FileOutputStream fos = null;/*from   w  ww.ja v a  2s  .com*/
    boolean result;
    try {
        // Connect to the localhost
        ftpClient.connect("localhost");

        // login to ftp server
        result = ftpClient.login("", "");
        if (result == true) {
            System.out.println("Successfully logged in!");
        } else {
            System.out.println("Login Fail!");
            return;
        }
        String fileName = "uploadfile.txt";
        fos = new FileOutputStream(fileName);

        // Download file from the ftp server
        result = ftpClient.retrieveFile(fileName, fos);

        if (result == true) {
            System.out.println("File downloaded successfully !");
        } else {
            System.out.println("File downloading failed !");
        }
        ftpClient.logout();
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
            ftpClient.disconnect();
        } catch (FTPConnectionClosedException e) {
            System.err.println(e);
        }
    }
}