The example below shows you how to connect to an FTP server. In this example we are using theFTPClient
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.
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();
}
}
}
}
Monday, March 5, 2012
How do I connect to FTP server in Java?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment