当另一个客户端使用(删除/移动)文件时,FTP 客户端(org.apache.commons.net.ftp.FTPClient)挂断,而 FTP 仍在上传。 这是我上传文件的代码:
ftpClient = getFTPConnection();
BufferedInputStream buffIn = new BufferedInputStream(inputStream);
byte[] buffer = new byte[8192];
OutputStream os = ftpClient.storeFileStream(filename);
int readCount = 0;
while ((readCount = buffIn.read(buffer)) > 0) {
os.write(buffer, 0, readCount);
}
os.close();
buffIn.close();
inputStream.close();
ftpClient.completePendingCommand(); //!stuck here!
ftpClient.rename(filename, "uploadRename.txt");
ftpClient.logout();
ftpClient.disconnect();
当文件在上传时被删除,os.write 完成它的工作(没问题。)但是在调用 completePendingCommand() 之后程序仍然卡住。 如果我理解正确,则必须完成 ftpClient.completePendingCommand() 才能发出第二个命令,删除它不是解决方案。 任何人都知道为什么 ftpClient 挂起? 如果 completePendingCommand 没有从 ftp 获得他想要的内容,我只是期待一条错误消息或超时。
谢谢
最佳答案
我试图重现您的情况,但是当我尝试在上传过程中修改文件时,服务器回复了 450 代码并且不允许删除文件。
顺便说一下,由于 Commons-net API 文档有点生硬,您可以尝试在 completePendingCommand 之前调用
调用。ftp.getReplyCode()
方法来找出到底发生了什么()
然后,可以匹配this table中的回复码并决定如何“切换”回复代码。
关于java - apache FTPClient 在上传时删除 FTP 上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13765262/