FTP Server : Connect and Login

Ads
 

FTP Server : Connect and Login

In this tutorial we will discuss how to connect and login in FTP server with java programming.

In this tutorial we will discuss how to connect and login in FTP server with java programming.

FTP Server : Connect and Login

In this tutorial we will discuss how to connect and login in FTP server with java programming.

FTP Server :

By using FTP Server you can transfer files from one computer to another computer through network and internet. For FTP server, it is required to authenticate user by inputting the user name and password.

Connect and Login : 

For connecting to the ftp server we need some APIs which is provided by the class FTPClient(org.apache.commons.net.ftp.FTPClient).
For connecting to the server FTPClient provides connect(String hostname) method. For local machine hostname is "localhost"

client.connect("hosetname");

For login we can use login() method. This method returns boolean value. If you logged in then it will return true else it will return false.

client.login("username","password");

Example :  In this example we are connecting to the ftp server. hostname is "localhost".

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

class FtpConnect {
	public static void main(String[] args) throws IOException {
		FTPClient client = new FTPClient();
		try {
			client.connect("localhost");
			boolean login = client.login("deepak", "deepak");

			if (login == true) {
				System.out.println("Successfully logged in!");
			} else {
				System.out.println("Login Fail!");
			}
		} catch (FTPConnectionClosedException e) {
			e.printStackTrace();
		} finally {
			try {
				client.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.out.println(e);
			}
		}
	}
}

Output :

Successfully logged in!

Ads

Ads