Java FTP Send commands to FTP server
- Details
- Written by Nam Ha Minh
- Last Updated on 19 July 2019   |   Print Email
In this article, we are going to show some examples that send FTP commands to a remote FTP server using the Apache Commons Net library. Basically, the FTP protocol (RFC 959) defines a set of standard commands which governs the communications between the client and the server. The client sends a command and the server returns a response either via the control channel or data channel (if a data connection is established). Behind the scenes, the Apache Commons Net library encapsulates and hides all these command-level details in order to expose high-level interfaces to the users. Therefore, it’s very rarely that we need to send the raw FTP commands. However, the Apache Commons Net API still provides such functionality if needed, e.g. in cases of some FTP servers define their own commands other than the standard ones.
Typically, the syntax for sending a FTP command from the client looks like this:
CommandName argument_1 argument_2, …
For example, the following command creates a directory named “Upload”:
MKD Upload
If the operation succeeds, the server returns a response looks like this:
257 "Upload" : The directory was successfully created
Depending on the type of command, the server returns the response either via control channel or data channel (e.g. file transfer commands). For simplicity, this article only deals with the response sent through the control channel, which can be retrieved via the getReplyString() or getReplyStrings() methods of the FTPClient class.
To send a FTP command, we can use the sendCommand() methods provided by the FTPClient class. There are several forms of this method, but the most convenient one is:
int sendCommand(FTPCmd command, String args)
This method is available since version 3.3 of the Commons Net library. The FTPCmd is an enum type that defines all the supported commands. For example, the following code snippet sends a MKD command to create a directory named “Upload”:
int replyCode = ftpClient.sendCommand(FTPCmd.MKD, "Upload"); if (!FTPReply.isPositiveCompletion(replyCode)) { System.out.println("The operation did not succeed!"); } else { System.out.println(ftpClient.getReplyString()); }
Here’s the full source code of a demo program that sends several commands in order to login, change current working directory, create a new directory, and then disconnect. The commands are used: USER, PASS, CWD, MKD and QUIT.
package net.codejava.ftp; import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPCmd; /** * This program demonstrates how to send commands to a remote FTP server * using the Apache Commons Net API. * @author www.codejava.net * */ public class FTPSendCommandsExample { public static void main(String[] args) { String server = "www.yourserver.com"; int port = 21; String user = "your_username"; String pass = "your_password"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); System.out.println(ftpClient.getReplyString()); ftpClient.sendCommand(FTPCmd.USER, user); System.out.println(ftpClient.getReplyString()); ftpClient.sendCommand(FTPCmd.PASS, pass); System.out.println(ftpClient.getReplyString()); ftpClient.sendCommand(FTPCmd.CWD, "Upload"); System.out.println(ftpClient.getReplyString()); ftpClient.sendCommand(FTPCmd.MKD, "CodeJava"); ftpClient.sendCommand(FTPCmd.QUIT, pass); System.out.println(ftpClient.getReplyString()); } catch (IOException ex) { System.err.println(ex); } } }
The above program would produce the following output when running:
220---------- Welcome to Pure-FTPd [privsep] [TLS] ---------- 220-You are user number 11 of 50 allowed. 220-Local time is now 04:23. Server port: 21. 220 You will be disconnected after 15 minutes of inactivity. 331 User uploader@codejava.net OK. Password required 230 OK. Current restricted directory is / 250 OK. Current directory is /Upload 221-Goodbye. You uploaded 0 and downloaded 0 kbytes. 221 Logout.
For a list of FTP commands and reply codes, see the links provided in the References section below.
References:
Related Java FTP Tutorials:
- Connect and login to a FTP server
- Java FTP example - Get server's reply messages
- Java FTP create directory example
- Java FTP example - Change working directory
- Java FTP list files and directories example
- Java FTP file download tutorial and example
- Java FTP file upload tutorial and example
- Java FTP rename file or directory example
- Java FTP delete file example
Comments
How can I send close_notify through your above code. plz reply ASAP.