【问题标题】:Delete file by filename on FTP with Java使用Java在FTP上按文件名删除文件
【发布时间】:2017-03-17 15:58:27
【问题描述】:

是否可以按文件名删除文件,无需扩展名。我正在使用 FTPClient 连接到我的 FTP 服务器并且工作得很好。我只能上传 3 种格式的文件(.png、.jpg、.gif)。事实上,只有当我指定这样的扩展名并工作时,我才能删除该文件:

ftp.deleteFile("/"+productID+setFileName+".png");

但是无论文件的扩展名是什么,我都想删除文件,只能通过文件名。谢谢

【问题讨论】:

标签: java ftp ftp-client


【解决方案1】:

startsWith() 函数怎么样?

org.apache.commons.net.ftp.FTPClient ftpClient=new FTPClient();  //instantiate the FTPClient
FTPFile[] ftpFiles=ftpClient.listFiles();//get the list of files in the root directory of the FTP server
for(FTPFile tempFtpFile:ftpFiles)
{
  //go through the list of files and delete those that start with your required prefix
  String tempFtpFileName=tempFtpFile.getName();
  if(tempFtpFileName.startsWith(productID+setFileName))
   ftpClient.deleteFile(tempFtpFile.getName());
}

【讨论】:

    【解决方案2】:

    这取决于您的 ftp 客户端是否实现了 ftp 命令mdelete

    AFAIK mdelete 接受通配符。或者,如果您的服务器在执行删除命令时接受通配符。

    您是否尝试过执行:

    ftp.deleteFile("/"+productID+setFileName+".*");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多