How do I connect to FTP server?

Category: commons.net, viewed: 13254 time(s).

The example below shows you how to connect to an FTP server. In this example we are using the FTPClient class of the Apache Commons Net. To connect to the server we need to provide the FTP server name. Login to the server can be done by calling the login() method of this class with a valid username and password. To logout we call the logout() method.

package org.kodejava.example.commons.net;

import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;

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

        try {
            client.connect("ftp.domain.com");

            //
            // When login success the login method returns true.
            //
            boolean login = client.login("admin", "secret");

            if (login) {
                System.out.println("Login success...");

                //
                // When logout success the logout method returns true.
                //
                boolean logout = client.logout();
                if (logout) {
                    System.out.println("Logout from FTP server...");
                }
            } else {
                System.out.println("Login fail...");
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //
                // Closes the connection to the FTP server
                //
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats