# FTP (File Transfer Protocol)
To start using FTP with Java, you will need to create a new FTPClient and then connect and login to the server using .connect(String server, int port) and .login(String username, String password) .
import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; //Import all the required resource for this project. public class FTPConnectAndLogin public static void main(String[] args) // SET THESE TO MATCH YOUR FTP SERVER // String server = "www.server.com"; //Server can be either host name or IP address. int port = 21; String user = "Username"; String pass = "Password"; FTPClient ftp = new FTPClient; ftp.connect(server, port); ftp.login(user, pass); > >
Now we have the basics done. But what if we have an error connecting to the server? We’ll want to know when something goes wrong and get the error message. Let’s add some code to catch errors while connecting.
try ftp.connect(server, port); showServerReply(ftp); int replyCode = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) System.out.printIn("Operation failed. Server reply code: " + replyCode) return; > ftp.login(user, pass); > catch >
Let’s break down what we just did, step by step.
This refers to a function we will be making in a later step.
int replyCode = ftp.getReplyCode();
This grabs the reply/error code from the server and stores it as an integer.
if (!FTPReply.isPositiveCompletion(replyCode)) System.out.printIn("Operation failed. Server reply code: " + replyCode) return; >
This checks the reply code to see if there was an error. If there was an error, it will simply print «Operation failed. Server reply code: » followed by the error code. We also added a try/catch block which we will add to in the next step. Next, let’s also create a function that checks ftp.login() for errors.
boolean success = ftp.login(user, pass); showServerReply(ftp); if (!success) System.out.println("Failed to log into the server"); return; > else System.out.println("LOGGED IN SERVER"); >
Let’s break this block down too.
boolean success = ftp.login(user, pass);
This will not just attempt to login to the FTP server, it will also store the result as a boolean.
This will check if the server sent us any messages, but we will first need to create the function in the next step.
if (!success) System.out.println("Failed to log into the server"); return; > else System.out.println("LOGGED IN SERVER"); >
This statement will check if we logged in successfully; if so, it will print «LOGGED IN SERVER», otherwise it will print «Failed to log into the server». This is our script so far:
import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class FTPConnectAndLogin public static void main(String[] args) // SET THESE TO MATCH YOUR FTP SERVER // String server = "www.server.com"; int port = 21; String user = "username" String pass = "password" FTPClient ftp = new FTPClient try ftp.connect(server, port) showServerReply(ftp); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) System.out.println("Operation failed. Server reply code: " + replyCode); return; > boolean success = ftp.login(user, pass); showServerReply(ftp); if (!success) System.out.println("Failed to log into the server"); return; > else System.out.println("LOGGED IN SERVER"); > > catch > > >
Now next let’s create complete the Catch block in case we run into any errors with the whole process.
> catch (IOException ex) System.out.println("Oops! Something went wrong."); ex.printStackTrace(); >
The completed catch block will now print «Oops! Something went wrong.» and the stacktrace if there is an error. Now our final step is to create the showServerReply() we have been using for a while now.
private static void showServerReply(FTPClient ftp) String[] replies = ftp.getReplyStrings(); if (replies != null && replies.length > 0) for (String aReply : replies) System.out.println("SERVER: " + aReply); > > >
This function takes an FTPClient as a variable, and calls it «ftp». After that it stores any server replies from the server in a string array. Next it checks if any messages were stored. If there is any, it prints each of them as «SERVER: [reply]». Now that we have that function done, this is the completed script:
import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class FTPConnectAndLogin private static void showServerReply(FTPClient ftp) String[] replies = ftp.getReplyStrings(); if (replies != null && replies.length > 0) for (String aReply : replies) System.out.println("SERVER: " + aReply); > > > public static void main(String[] args) // SET THESE TO MATCH YOUR FTP SERVER // String server = "www.server.com"; int port = 21; String user = "username" String pass = "password" FTPClient ftp = new FTPClient try ftp.connect(server, port) showServerReply(ftp); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) System.out.println("Operation failed. Server reply code: " + replyCode); return; > boolean success = ftp.login(user, pass); showServerReply(ftp); if (!success) System.out.println("Failed to log into the server"); return; > else System.out.println("LOGGED IN SERVER"); > > catch (IOException ex) System.out.println("Oops! Something went wrong."); ex.printStackTrace(); > > >
We first need to create a new FTPClient and try connecting to the server it and logging into it using .connect(String server, int port) and .login(String username, String password) . It is important to connect and login using a try/catch block in case our code fails to connect with the server. We will also need to create a function that checks and displays any messages we may receive from the server as we try connecting and logging in. We will call this function » showServerReply(FTPClient ftp) «.
import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class FTPConnectAndLogin private static void showServerReply(FTPClient ftp) if (replies != null && replies.length > 0) for (String aReply : replies) System.out.println("SERVER: " + aReply); > > > public static void main(String[] args) // SET THESE TO MATCH YOUR FTP SERVER // String server = "www.server.com"; int port = 21; String user = "username" String pass = "password" FTPClient ftp = new FTPClient try ftp.connect(server, port) showServerReply(ftp); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) System.out.println("Operation failed. Server reply code: " + replyCode); return; > boolean success = ftp.login(user, pass); showServerReply(ftp); if (!success) System.out.println("Failed to log into the server"); return; > else System.out.println("LOGGED IN SERVER"); > > catch (IOException ex) System.out.println("Oops! Something went wrong."); ex.printStackTrace(); > > >
After this, you should now have your FTP server connected to you Java script.
# Syntax
- FTPClient connect(InetAddress host, int port)
- FTPClient login(String username, String password)
- FTPClient disconnect()
- FTPReply getReplyStrings()
- boolean storeFile(String remote, InputStream local)
- OutputStream storeFileStream(String remote)
- boolean setFileType(int fileType)
- boolean completePendingCommand()
# Parameters
Parameters | Details |
---|---|
host | Either the host name or IP address of the FTP server |
port | The FTP server port |
username | The FTP server username |
password | The FTP server password |
Java Connect and Login to FTP server
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:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.