Home Java Javaftp FTP Server : Connect and Login
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

FTP Server : Connect and Login

Advertisement
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!
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 4, 2012

Related Tutorials

Ask Questions?    Discuss: FTP Server : Connect and Login  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Nghia
May 27, 2013
localhost

How do you create localhost? and i code in neatbean and what do i need to connect this localhost??
DMCA.com