nginx http 流下载 断点下载

public class TestHttpResume {

    public static void main(String[] args) throws Exception {
        BufferedOutputStream output = null;
        BufferedInputStream br = null;
        URL url = new URL("http://IP/test.war");
        HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); // 设置 User-Agent
        httpConnection.setRequestProperty("User-Agent", "NetFox"); // 设置断点续传的开始位置
        httpConnection.setRequestProperty("RANGE", "bytes=0-100"); // 获得输入流
        InputStream input = httpConnection.getInputStream();

        RandomAccessFile file = new RandomAccessFile(new File("D://lms3.war"), "rw");
        long nPos = 0;
        // 定位文件指针到 nPos 位置
        file.seek(0);
        byte[] b = new byte[1024];
        int nRead; // 从输入流中读入字节流,然后写到文件中
        while ((nRead = input.read(b)) > 0) {
            file.write(b, 0, nRead);
        }

    }
}

实现前100 的 下载

 

之后修改httpConnection.setRequestProperty("RANGE", "bytes=0-100"); // 获得输入流
 为100-

文件定位到   file.seek(100);

至此文件完结 

实际可动态修改 参数 实现http文件流的 断点下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值