import java.io.IOException;After compiling and running the program, the terminal displays that the deletion was successful:
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class deleteFileFTP {
public static void main(String[] args) {
String server = "server name or address";
int port = 21;
String username = "username";
String password = "password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
int response = ftpClient.getReplyCode();
if (! FTPReply.isPositiveCompletion(answer)) {
System.out.println("Connection failed");
return;
}
boolean res = ftpClient.login(username, password);
if (res==false) {
System.out.println("Failed Server Identification");
return;
}
String fileToDelete = "/music/Symphonie.wma";
boolean isDeleted = ftpClient.deleteFile(fileToDelete);
if (isDeleted==true) {
System.out.println("The file has been successfully deleted");
} else {
System.out.println("Unable to delete file");
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
e.g.printStackTrace();
} finally {
// disconnect from the server
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
The file was successfully deleted
Please disable your ad blocker and refresh the window to use this website.