The FTPClient class provides the following three methods for requesting system type, system status and file/directory status:

The following is code of an example program that connects to a FTP server then queries system type, system status, directory status and file status:

package net.codejava.ftp;

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;

/**
 * This program demonstrates how to query status information from a FTP server.
 * @author www.codejava.net
 *
 */
public class FTPStatusDemo {

	public static void main(String[] args) {
		String server = "www.myserver.com";
		int port = 21;
		String user = "username";
		String pass = "password";

		FTPClient ftpClient = new FTPClient();

		try {
			ftpClient.connect(server, port);

			ftpClient.login(user, pass);

			System.out.println("SYSTEM TYPE:\n " + ftpClient.getSystemType());

			System.out.println("SYSTEM STATUS:\n " + ftpClient.getStatus());

			System.out.println("DIRECTORY STATUS:\n " + ftpClient.getStatus("/HNS"));

			System.out.println("FILE STATUS:\n " + ftpClient.getStatus("/Test/PIC4.JPG"));

			ftpClient.logout();

			ftpClient.disconnect();

		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (ftpClient.isConnected()) {
				try {
					ftpClient.disconnect();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}
	}
}

Sample output of the above program:

SYSTEM TYPE:
 UNIX Type: L8
SYSTEM STATUS:
 211 http://www.pureftpd.org/

DIRECTORY STATUS:
 213-STAT
drwxr-xr-x    8 tom      tom            4096 Oct 24  2012 .
drwxr-xr-x   17 tom      tom            4096 Aug  5 07:01 ..
drwxr-xr-x    2 tom      tom            4096 Jul 28 04:46 Blog
drwxr-xr-x    2 tom      tom            4096 Aug  5 00:18 Forum
drwxr-xr-x    3 tom      tom            4096 Oct  3  2012 Freelance
drwxr-xr-x    5 tom      tom            4096 Jul 28 04:50 Projects
drwxr-xr-x   21 tom      tom            4096 Jul 28 07:34 Website
drwxr-xr-x    3 tom      tom            4096 Sep 12  2012 Writing
213 End.

FILE STATUS:
 213-STAT
-rw-r--r--    1 tom      tom         2641068 Jul 26 23:42 /Test/PIC4.JPG
213 End.

 

Other Java FTP Tutorials:


About the Author:

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.



Attachments:
Download this file (FTPStatusDemo.java)FTPStatusDemo.java[Demo program]1 kB