You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JSch的ChannelSftp断开连接后恢复文件下载

JSch ChannelSftp 断点续传实现方案

ChannelSftp本身支持断点续传,无需完全自行编写下载逻辑,只需利用其重载的get方法并配合断点检测逻辑即可实现。

核心思路

断点续传的关键是:

  • 记录本地已下载的文件字节数
  • 从服务器文件的对应偏移位置开始下载,将数据追加到本地文件末尾

具体实现步骤及代码

  1. 检测本地文件状态:如果本地已存在待下载文件,获取其已下载的大小
  2. 获取服务器文件信息:确认服务器源文件的总大小,判断是否可以续传
  3. 执行断点续传:调用带偏移量的get方法,从断点位置开始下载并追加到本地文件
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class SftpResumeDownload {
    public static void resumeDownload(ChannelSftp channelSftp, String source, String destination) throws SftpException {
        File localFile = new File(destination);
        long downloadedSize = 0;

        // 检查本地文件是否存在,获取已下载大小
        if (localFile.exists()) {
            downloadedSize = localFile.length();
            System.out.println("检测到本地已下载 " + downloadedSize + " 字节,准备续传");
        }

        // 获取服务器文件的总大小
        SftpATTRS remoteAttrs = channelSftp.stat(source);
        long remoteFileSize = remoteAttrs.getSize();

        if (downloadedSize < remoteFileSize) {
            // 以追加模式打开本地文件输出流
            try (OutputStream outputStream = new FileOutputStream(localFile, true)) {
                // 调用带偏移量的get方法,从downloadedSize位置开始下载
                channelSftp.get(source, outputStream, downloadedSize);
                System.out.println("续传完成,总下载大小:" + localFile.length());
            } catch (Exception e) {
                throw new RuntimeException("续传过程中发生错误", e);
            }
        } else if (downloadedSize == remoteFileSize) {
            System.out.println("文件已完全下载,无需续传");
        } else {
            System.err.println("本地文件大小大于服务器文件,可能文件已损坏或服务器文件被修改");
        }
    }
}

注意事项

  • 连接中断重连:如果续传过程中再次断开连接,需要重新建立ChannelSftp连接后,重复上述续传逻辑即可
  • 文件一致性校验:若服务器文件在中断后被修改(如大小变小、内容变更),续传可能导致文件损坏,建议添加文件校验逻辑(如MD5对比)
  • 异常处理:需捕获SftpException(如文件不存在、权限不足)和IO异常,避免程序崩溃

内容的提问来源于stack exchange,提问作者user10597386

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 08:57:14