Java Apache FTP客户端-恢复中断的上传

时间:2018-09-26 12:21:02

标签: java ftp upload ftp-client apache-commons-net

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

1 个答案:

答案 0 :(得分:1)

  • 重新连接您的FTP会话(如果它也已断开);
  • 测试远程文件的大小,以确定一直到远程磁盘的字节数(例如,使用FTPClient.mlistFileSIZE命令-请参见How to get info of an FTPFile);
  • 将“输入流”搜索到该点(尽管InputStream不支持查找,因此您将不得不使用其他流实现-或重新打开InputStreamskip到该位置);
  • 致电FTPClient.appendFile