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) throws SocketException, IOException 

Source Link

Document

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

Usage

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  .j a  va  2s.com*/

    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) {/*from  w  ww .java  2s  .  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: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  ww. j av 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:Main.java

public static void main(String[] args) {
    FTPClient client = new FTPClient();
    FileInputStream fis = null;/*from   w  ww  .ja va 2  s  .  c o m*/

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

    String filename = "Touch.dat";
    fis = new FileInputStream(filename);
    client.storeFile(filename, fis);
    client.logout();
    fis.close();
}

From source file:Main.java

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

    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:ftp_server.FileUploadDemo.java

public static void main(String[] args) {
    FTPClient client = new FTPClient();
    FileInputStream fis = null;/*from ww  w . ja va2s .c o m*/
    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:FTPConnect.FTPConnect.java

public static void main(String[] args) throws IOException {
    FTPClient ftpClient = new FTPClient();
    boolean result;
    try {/*ww  w .  j  ava 2  s.  co  m*/
        // 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:FTPConnect.FTPBajarArchivo.java

public static void main(String[] args) throws IOException {
    FTPClient ftpClient = new FTPClient();
    FileOutputStream fos = null;// www  .java2 s  .  co m
    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);
        }
    }
}

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());

    // After connection attempt, you should check the reply code to verify
    // success./*from  w w w . j a  v  a  2  s  .com*/
    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.FTPSubirArchivo.java

public static void main(String[] args) throws IOException {
    FTPClient ftpclient = new FTPClient();
    FileInputStream fis = null;//from   w w w  . j  a  va2 s. c  om
    boolean result;
    String ftpServerAddress = "localhost";
    String userName = "admin";
    String password = "admin";

    try {
        ftpclient.connect(ftpServerAddress);
        result = ftpclient.login(userName, password);

        if (result == true) {
            System.out.println("Logged in Successfully !");
        } else {
            System.out.println("Login Fail!");
            return;
        }
        ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

        ftpclient.changeWorkingDirectory("/");

        File file = new File("D:/File.doc");
        String testName = file.getName();
        fis = new FileInputStream(file);

        // Upload file to the ftp server
        result = ftpclient.storeFile(testName, fis);

        if (result == true) {
            System.out.println("File is uploaded successfully");
        } else {
            System.out.println("File uploading failed");
        }
        ftpclient.logout();
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } finally {
        try {
            ftpclient.disconnect();
        } catch (FTPConnectionClosedException e) {
            System.out.println(e);
        }
    }
}