关于FTPClient操作文件后无法变更目录的问题。

FTPClient我用的是apache的。


这就如同你在win下打开了一个文件,但是又要删除那个文件是一个道理。(可以尝试一下,打开一个文件,不关闭,再删除它,一定是不能删除的。)


我这边是用流接收的文件进行操作,所以需要关闭流。然后使用completPendingxxx方法,忘记了。等待完成这次操作。然后在rename就可以成功了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Java语言实现FTPClient删除远程服务器上的文件目录,可以按照以下步骤进行操作: 1. 创建FTPClient对象,并连接到FTP服务器。 2. 使用FTPClient的login()方法登录FTP服务器。 3. 使用FTPClient的changeWorkingDirectory()方法切换到要删除的目录。 4. 使用FTPClient的deleteFile()方法删除指定的文件。 5. 使用FTPClient的removeDirectory()方法删除指定的目录。 6. 关闭FTP连接。 以下是Java代码示例: ``` import org.apache.commons.net.ftp.FTPClient; import java.io.IOException; public class FTPDeleteExample { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String user = "username"; String pass = "password"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(user, pass); String directoryPath = "/example/directory"; if (ftpClient.changeWorkingDirectory(directoryPath)) { String fileName = "example.txt"; boolean deleted = ftpClient.deleteFile(fileName); if (deleted) { System.out.println("File " + fileName + " deleted successfully."); } else { System.out.println("Failed to delete file " + fileName + "."); } } else { System.out.println("Directory " + directoryPath + " not found."); } String emptyDirectoryPath = "/empty/directory"; if (ftpClient.changeWorkingDirectory(emptyDirectoryPath)) { boolean removed = ftpClient.removeDirectory(emptyDirectoryPath); if (removed) { System.out.println("Directory " + emptyDirectoryPath + " removed successfully."); } else { System.out.println("Failed to remove directory " + emptyDirectoryPath + "."); } } else { System.out.println("Directory " + emptyDirectoryPath + " not found."); } ftpClient.logout(); } catch (IOException ex) { System.err.println("Error: " + ex.getMessage()); } finally { try { ftpClient.disconnect(); } catch (IOException ex) { System.err.println("Error: " + ex.getMessage()); } } } } ``` 在上面的代码中,使用FTPClient连接到FTP服务器,然后使用changeWorkingDirectory()方法切换到要删除的目录,并使用deleteFile()方法删除指定的文件,或使用removeDirectory()方法删除指定的目录。最后,关闭FTP连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值