Java Connect and Login to FTP server
- Details
- Written by Nam Ha Minh
- Last Updated on 19 July 2019   |   Print Email
This article provides Java code examples that demonstrate how to connect and login to a FTP server, using Apache Commons Net library. Make sure you have jar file commons-net-VERSION.jar available in your project’s classpath.
The class FTPClient (org.apache.commons.net.ftp.FTPClient) provides necessary APIs to work with a server via FTP protocol. To connect to a server, use this method:
void connect(String server, int port)
Where server can be either host name or IP address, and port is a number (FTP protocol is using port number 21).
After connected, use this method to login:
boolean login(String username, String password)
The login() method returns true if login successfully, false if not.
It’s advisable to check server’s reply code after each call of void method such as the connect() method above, for example:
int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { // oops! The operation was not successful // for some reasons, the server refuses or // rejects requested operation return; }
After each method call, the server may return back some messages. The following method displays server’s messages in the standard output:
private static void showServerReply(FTPClient ftpClient) { String[] replies = ftpClient.getReplyStrings(); if (replies != null && replies.length > 0) { for (String aReply : replies) { System.out.println("SERVER: " + aReply); } } }
The following code is for an example program:
import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class FTPConnectAndLoginDemo { private static void showServerReply(FTPClient ftpClient) { String[] replies = ftpClient.getReplyStrings(); if (replies != null && replies.length > 0) { for (String aReply : replies) { System.out.println("SERVER: " + aReply); } } } public static void main(String[] args) { String server = "www.yourserver.net"; int port = 21; String user = "username"; String pass = "password"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); showServerReply(ftpClient); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { System.out.println("Operation failed. Server reply code: " + replyCode); return; } boolean success = ftpClient.login(user, pass); showServerReply(ftpClient); if (!success) { System.out.println("Could not login to the server"); return; } else { System.out.println("LOGGED IN SERVER"); } } catch (IOException ex) { System.out.println("Oops! Something wrong happened"); ex.printStackTrace(); } } }
Compile and run the example program:
Related Java FTP Tutorials:
- Java FTP create directory example
- Java FTP example - Change working directory
- Java FTP list files and directories example
- Java FTP file download tutorial and example
- Java FTP file upload tutorial and example
- Java FTP rename file or directory example
- Java FTP delete file example
Comments
it shows this error !
Please help me sir !
but it shows error.
Code:::
String server = "192.168.1.101";
int port = 21;
String user = "user";
String pass = "infotech";
Error:::
Have you found out any solution for this
Could you suggest me how to get out from this.