Java FTP Delete a file on server example
- Details
- Written by Nam Ha Minh
- Last Updated on 19 July 2019   |   Print Email
To delete an existing file on a FTP server using Apache Commons Net API, we can use method deleteFile() of FTPClient class. Signature of this method is as follows:
public boolean deleteFile(String pathname) throws IOException
The method will issue a FTP command DELE to the FTP server to delete the remote file specified by pathname. It returns true if the remote file is successfully deleted, or false otherwise (i.e the file does not exist or is a directory). In case of exception, the method throws an FTPConnectionClosedException exception if connection with the server is already closed, or an IOException exception if there is an I/O error occurs during communication with the server.
Here is an usage example for the method deleteFile():
FTPClient ftpClient = new FTPClient(); // code to connect and login... String fileToDelete = "/Path/Of/File/To/Delete"; try { boolean deleted = ftpClient.deleteFile(fileToDelete); if (deleted) { System.out.println("The file was deleted successfully."); } else { System.out.println("Could not delete the file."); } } catch (IOException ex) { System.out.println("Oh no, there was an error: " + ex.getMessage()); } // code to log out and disconnect...
To illustrate usage of the deleteFile() method, we provide a working example program which:
- Logins to a FTP server
- Deletes a file on the server
- Logs out and disconnects
Here is the source code:
import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class FTPDeleteFileDemo { public static void main(String[] args) { String server = "www.myserver.com"; int port = 21; String user = "user"; String pass = "pass"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { System.out.println("Connect failed"); return; } boolean success = ftpClient.login(user, pass); if (!success) { System.out.println("Could not login to the server"); return; } String fileToDelete = "/repository/video/cool.mp4"; boolean deleted = ftpClient.deleteFile(fileToDelete); if (deleted) { System.out.println("The file was deleted successfully."); } else { System.out.println("Could not delete the file, it may not exist."); } } catch (IOException ex) { System.out.println("Oh no, there was an error: " + ex.getMessage()); ex.printStackTrace(); } finally { // logs out and disconnects from server try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } } }
NOTE: To use Apache Commons Net API, you must have commons-net-VERSION.jar file available in the classpath. You can download latest distribution of Apache Commons Net API at: http://commons.apache.org/net/download_net.cgi
Related Java FTP Tutorials:
- Determine if a directory or file exists on FTP server
- Get size of a file on FTP server
- Java FTP example - Get and set file modification time
- Java FTP example - Get details of a file or directory on server
- Rename file or directory on FTP server
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
I have used the same code , but getting following exception
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.