Java Apache FTP 客户端 - 恢复损坏的上传

问题描述 投票:0回答:1

如果出现问题,我需要在 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
继续上传?

java ftp upload ftp-client apache-commons-net
1个回答
1
投票
  • 重新连接您的 FTP 会话(如果它也中断了);
  • 测试远程文件的大小,以确定有多少字节一直到达远程磁盘(例如使用
    FTPClient.mlistFile
    SIZE
    命令 – 请参阅如何获取 FTPFile 的信息);
  • 将“输入流”返回到该点(尽管
    InputStream
    不支持查找,因此您必须使用不同的流实现 - 或者重新打开
    InputStream
    skip
    到该位置);
  • 致电
    FTPClient.appendFile
© www.soinside.com 2019 - 2024. All rights reserved.