我的应用程序将 txt 文件存储在 FTP 服务器上,该服务器也托管在网络服务上。
在我托管 txt 文件的目录中,我可以找到其他 txt 文件。我想每次存储新文件时都删除当前目录中的所有文件。
实际上我试图使用以下命令:
FTPClient ftpClient = new FTPClient();
ftpClient.connect(siteFTP);
if (ftpClient.login(usrFTP, pswFTP)) {
ftpClient.enterLocalPassiveMode();
FTPFile[] remoteFiles = ftpClient.listFiles(path);
if (remoteFiles.length > 0) {
ftpClient.deleteFile("/prenotazioni/*.txt");
}
}
但是即使该目录中有txt文件,FTP响应也是:
> DELE /prenotazioni/*.txt
> 550 File not found
最佳答案
使用 *
不起作用。获得声明目录中的文件列表后,必须迭代它并使用 deleteFile(String pathname)
逐个删除文件(同时检查文件名 endsWith(".txt")
)。
每个FTPFile
都有方法getName()
。您应该构建完整路径,以便 FTPClient 知道要删除哪个文件。我相信它会是这样的:
ftpClient.deleteFile("/prenotazioni/" + remoteFiles[i].getName());
关于java - 如何删除FTP目录中的所有txt文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56865979/