Java FTP example - Get status of system, directory and file
- Details
- Written by Nam Ha Minh
- Last Updated on 19 July 2019   |   Print Email
The FTPClient class provides the following three methods for requesting system type, system status and file/directory status:
- String getSystemType(): returns system type of the currently connected FTP server.
- Example:
String systemType = ftpClient.getSystemType(); System.out.println(systemType);
- Sample output:
UNIX Type: L8
- Example:
- String getStatus(): gets system status information returned from the FTP server.
- Example:
String systemStatus = ftpClient.getStatus(); System.out.println(systemStatus);
- Sample output:
211 http://www.pureftpd.org/
- Example:
- String getStatus(String pathname): gets status of a given pathname on the server which results in a Unix-style file/directory listing.
- Example:
String dirPath = "/Upload"; System.out.println(ftpClient.getStatus(dirPath));
- Sample output:
213-STAT drwxr-xr-x 5 tom tom 4096 Apr 16 23:02 . drwxr-xr-x 17 tom tom 4096 Aug 5 07:01 .. drwxr-xr-x 4 tom tom 4096 Mar 14 22:51 Java drwxr-xr-x 4 tom tom 4096 Mar 14 22:51 PHP 213 End.
- Example:
- String getSystemType(): returns system type of the currently connected FTP server.
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:
- Connect and login to a FTP server
- Java FTP example - Change working directory
- Java FTP create directory example
- Java FTP list files and directories example
- Java FTP file download tutorial and example
- Java FTP file upload tutorial and example
- Java FTP example - Search for files and directories
- How to upload a directory to a FTP server
- How to download a complete folder from a FTP server
Comments