import java.io.IOException;编译运行程序后,终端显示删除成功:
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 = 服务器名称或地址”;
int 端口 = 21;
字符串 username = username”;
字符串 password = password”;
FTPClient ftpClient = 新的 FTPClient();
try {
ftpClient.connect(server, port);
int response = ftpClient.getReplyCode();
如果 (!FTPReply.isPositiveCompletion(answer)) {
System.out.println(连接失败”);
返回;
}
boolean res = ftpClient.login(用户名,密码);
if (res==false) {
System.out.println(服务器标识失败”);
返回;
}
String fileToDelete = /music/Symphonie.wma”;
布尔值 isDeleted = ftpClient.deleteFile(fileToDelete);
if (isDeleted==true) {
System.out.println(文件已成功删除”);
} else {
System.out.println(无法删除文件”);
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
例如 printStackTrace();
} finally {
// 断开与服务器的连接
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
文件删除成功
Please disable your ad blocker and refresh the window to use this website.