As know FTP is a reliable protocol used for transferring files over the Internet. Though it is not a secure protocol, and all the data is transmitted in clear text. But it is reliable enough protocol and send the data from a sender to receiver over Internet.
The code talks about getting the names of the all the files you wish to read from on a FTP server using the following process:
- In Java, we will use the FTPClient object to read files from FTP server. The class, ‘org.apache.commons.net.ftp.FTPClient’, needs to be imported to create an instance of FTPClient.
- The connect() method is called for the FTPClient object, with the URL as the parameter, to establish a connection with.
- Using the login() method send the credentials – user-name and password. For correct credentials and a successful login, true is returned which allows user to access files on the server.
- After the successful login, use the listNames() method to fetch the names of all the files in the current working directory. The list returned will be an array which can be iterated over to read the names.
- Since the login was a success, it requires a logout() after the reading of filenames has been done. Lastly, the disconnect() method is called to end the relation with the connected FTP server.
// Java code to illustrate // How to get a list of files from the FTPserver import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; public class MyFTPClass { public static void main(String args[]) { // Create an instance of FTPClient FTPClient ftp = new FTPClient(); try { // Establish a connection with the FTP URL ftp.connect("ftp.test.com"); // Enter user details : user name and password boolean isSuccess = ftp.login("user", "password"); if (isSuccess) { // Fetch the list of names of the files. In case of no files an // empty array is returned String[] filesFTP = ftp.listNames(); int count = 1; // Iterate on the returned list to obtain name of each file for (String file : filesFTP) { System.out.println("File " + count + " :" + file); count++; } } ftp.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } |
This article is contributed by Ritika. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java program to List all files in a directory and nested sub-directories | Recursive approach
- Jar files in Java
- How to rename all files of a folder using Java?
- Working with JAR and Manifest files In Java
- Compressing and Decompressing files in Java
- Files size() method in Java with Examples
- Files deleteIfExists() method in Java with Examples
- Files getFileStore() method in Java with Examples
- Files delete() method in Java with Examples
- Files isReadable() method in Java with Examples
- Java program to merge two files into a third file
- Files isWritable() method in Java with Examples
- Files isExecutable() method in Java with Examples
- Files isHidden() method in Java with Examples
- How to find and open the Hidden files in a Directory using Java
- Java program to merge two files alternatively into third file
- Compressing and Decompressing files using GZIP Format in java
- Java program to merge two files alternatively into third file
- Java program to merge contents of all the files in a directory
- Program to convert List of String to List of Integer in Java

