Besides standard commands defined by the FTP protocol (RFC 959), some FTP servers support extended features (called extensions). To know the extensions supported by a FTP server, we can call the features() method of the FTPClient class in the Apache Commons Net API as follows:

boolean success = ftpClient.features();
if (success) {
	System.out.println(ftpClient.getReplyString());
} else {
	System.out.println("Could not query server features");
}

The server’s response would be as follows (depending on which server):

211-Extensions supported:
 EPRT
 IDLE
 MDTM
 SIZE
 MFMT
 REST STREAM
 MLST type*;size*;sizd*;modify*;UNIX.mode*;UNIX.uid*;UNIX.gid*;unique*;
 MLSD
 AUTH TLS
 PBSZ
 PROT
 ESTA
 PASV
 EPSV
 SPSV
 ESTP
211 End.

That lists all extensions supported by the server. It’s also possible to check whether the server supports a specific feature, using the hasFeature() method, for example:

boolean success = ftpClient.hasFeature("SIZE");

if (success) {
	System.out.println("The server supports SIZE feature.");
} else {
	System.out.println("The server does not support SIZE feature.");
}

Some features have values, e.g. the AUTH feature (authentication type). To know value of a specific feature, call the featureValue() method, for example:

String featureValue = ftpClient.featureValue("AUTH");
if (featureValue == null) {
	System.out.println("The server does not support this feature.");
} else {
	System.out.println("Value of AUTH feature: " + featureValue);
}

The output would be (depending on server):

Value of AUTH feature: TLS

The featureValue() method will return an empty string if the given feature is present but has no value. It returns null if the feature is not supported or the command failed. 

Here’s an example program that connects to a FTP server, then queries its supported extensions, check whether it supports the SIZE command, and get value of the AUTH feature:

package net.codejava.ftp;

import java.io.IOException;

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

/**
 * This program demonstrates how to query for extensions (features)
 * supported by the currently connected FTP server.
 * @author www.codejava.net
 *
 */
public class FTPServerFeaturesDemo {

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

		FTPClient ftpClient = new FTPClient();

		try {
			ftpClient.connect(server, port);

			boolean success = ftpClient.features();
			if (success) {
				System.out.println(ftpClient.getReplyString());
			} else {
				System.out.println("Could not query server features");
			}

			success = ftpClient.hasFeature("SIZE");

			if (success) {
				System.out.println("The server supports SIZE feature.");
			} else {
				System.out.println("The server does not support SIZE feature.");
			}

			String featureValue = ftpClient.featureValue("AUTH");
			if (featureValue == null) {
				System.out.println("The server does not support this feature.");
			} else {
				System.out.println("Value of AUTH feature: " + featureValue);
			}

			ftpClient.disconnect();

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

NOTE: It does not require the client to be logged in when using the features(), hasFeature() and featureValue() methods, as shown in the above example program.

 

Other Java FTP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



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

Add comment