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: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 ww.  j av  a 2  s .c o m

    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  w  w .j av a  2 s  .  co  m*/
        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();
    FileInputStream fis = null;/*from  ww  w.java 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   w w w  . j av a  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: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  av a 2 s .  c om

    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;//w ww. j  a  va2  s  . 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:FTPConnect.FTPConnect.java

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

public static void main(String[] args) throws IOException {
    FTPClient ftpclient = new FTPClient();
    FileInputStream fis = null;/*  w w w  . j a v a  2s.c  o m*/
    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);
        }
    }
}

From source file:ftp_server.FTPFileUpload.java

public static void main(String[] args) throws IOException {
    FTPClient ftpclient = new FTPClient();
    FileInputStream fis = null;//  w  w  w.jav  a  2  s .c o  m
    boolean result;
    String ftpServerAddress = "shamalmahesh.net78.net";
    String userName = "a9959679";
    String password = "9P3IckDo";

    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.enterLocalPassiveMode();
        ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

        ftpclient.changeWorkingDirectory("/public_html/testing");

        File file = new File("D:/jpg/wq.jpg");
        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);
        }
    }
}