如果出现问题,我需要在 FTP 客户端中实现恢复上传。下面示例中的
ftp
是 Apache FTPClient
。
public boolean upload(InputStream localFile, String remoteName, boolean createNew) {
if (StringUtils.isBlank(remoteName)) {
log.warn("Error while uploading file: localFile or remoteName is null");
return false;
}
synchronized (this) {
try {
if (createNew) {
return ftp.storeFile(remoteName, localFile);
} else {
return ftp.appendFile(remoteName, localFile); //todo is it right?
}
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
return false;
}
}
}
因此,如果
ftp.storeFile
崩溃(例如,并非所有字节都已发送),我如何使用相同的 InputStream
继续上传?
FTPClient.mlistFile
或 SIZE
命令 – 请参阅如何获取 FTPFile 的信息);InputStream
不支持查找,因此您必须使用不同的流实现 - 或者重新打开 InputStream
和 skip
到该位置);FTPClient.appendFile
。