前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >断点续传的例子

断点续传的例子

作者头像
Dream城堡
发布2022-01-07 17:20:23
1.9K0
发布2022-01-07 17:20:23
举报
文章被收录于专栏:Spring相关Spring相关
代码语言:javascript
复制
package com.test.bsecond.y_2020.month_01.day0113;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;


public class DuanFile {

    public static void main(String[] args) {

        try {

            String path = "https://s2.ax1x.com/2020/01/08/lg8YLt.md.png";

            //(1) 创建一个url对象 参数就是网址
            URL url = new URL(path);
            //(2)获取HttpURLConnection 链接对象
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            //(3)设置参数  发送get请求
            conn.setRequestMethod("GET"); //默认请求 就是get  要大写
            //(4)设置链接网络的超时时间
            conn.setConnectTimeout(5000);
            //(5)获取服务器返回的状态码
            //200  代表获取服务器资源全部成功  206请求部分资源
            int code = conn.getResponseCode();
            if (code == 200) {
                // (6) 获取服务器文件的大小
                int length = conn.getContentLength();

                // [6.1] 把线程的数量赋值给正在运行的线程
                int threadCount = 3;
                System.out.println("length:" + length);

                //[二☆☆☆☆ ] 创建一个大小和服务器一模一样的文件 目的提前把空间申请出来
                String fileStr = "C:\\Users\\admin\\Desktop\\temp\\picCopy.jpg";
                RandomAccessFile rafAccessFile = new RandomAccessFile(fileStr, "rw");
                rafAccessFile.setLength(length);

                //(7)算出每个线程下载的大小
                int blockSize = length / threadCount;

                //[三☆☆☆☆  计算每个线程下载的开始位置和结束位置 ]
                for (int i = 0; i < threadCount; i++) {
                    int startIndex = i * blockSize;   //每个线程下载的开始位置
                    int endIndex = (i + 1) * blockSize - 1;
                    //特殊情况 就是最后一个线程
                    if (i == threadCount - 1) {
                        //说明是最后一个线程
                        endIndex = length - 1;

                    }

                    System.out.println("线程id:" + i + "理论下载的位置" + ":" + startIndex + "-----" + endIndex);

                    //四 开启线程去服务器下载文件
                    DownLoadThread downLoadThread = new DownLoadThread(startIndex, endIndex, i, threadCount, path);
                    downLoadThread.start();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

//定义线程去服务器下载文件
class DownLoadThread extends Thread {

    private String path;

//    String path = "https://s2.ax1x.com/2020/01/08/lg8YLt.md.png";

    String fileStr = "C:\\Users\\admin\\Desktop\\temp\\";
    //通过构造方法把每个线程下载的开始位置和结束位置传递进来
    private int startIndex;
    private int endIndex;
    private int threadId;


    //如果中断过  获取上次下载的位置
    private int pblastPostion;
    private int runningThread;

    public DownLoadThread(int startIndex, int endIndex, int threadId, int runningThread, String path) {
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.threadId = threadId;
        this.runningThread = runningThread;
        this.path = path;
    }

    @Override
    public void run() {
        //四  实现去服务器下载文件的逻辑
        try {
            //(1) 创建一个url对象 参数就是网址
            URL url = new URL(path);
            //(2)获取HttpURLConnection 链接对象
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //(3)设置参数  发送get请求
            conn.setRequestMethod("GET"); //默认请求 就是get  要大写
            //(4)设置链接网络的超时时间
            conn.setConnectTimeout(5000);

            String prefixStr = "C:\\Users\\admin\\Desktop\\temp\\";
            //[4.0]如果中间断过  继续上次的位置 继续下载   从文件中读取上次下载的位置
            File file = new File(prefixStr + threadId + ".txt");
            if (file.exists() && file.length() > 0) {
                FileInputStream fis = new FileInputStream(file);
                BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));
                String lastPositionn = bufr.readLine();  //读取出来的内容就是上一次下载的位置
                int lastPosition = Integer.parseInt(lastPositionn);
                //[4.0.1]要改变一下 startIndex 位置
                startIndex = lastPosition + 1;
                System.out.println("线程id::" + threadId + "真实下载的位置" + ":" + startIndex + "-----" + endIndex);
                fis.close(); //关闭流
            }

            //[4.1]设置一个请求头Range (作用告诉服务器每个线程下载的开始位置和结束位置)
            conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);

            //(5)获取服务器返回的状态码
            //200  代表获取服务器资源全部成功  206请求部分资源 成功
            int code = conn.getResponseCode();

            if (code == 206) {

                //[6]创建随机读写文件对象
                RandomAccessFile raf = new RandomAccessFile("C:\\Users\\admin\\Desktop\\temp\\bkkk.png", "rw");
                //[6]每个线程要从自己的位置开始写
                raf.seek(startIndex);

                InputStream in = conn.getInputStream(); //存的是feiq.exe

                //[7]把数据写到文件中
                int len = -1;
                byte[] buffer = new byte[1024 * 1024];//1Mb

                int total = 0; //代表当前线程下载的大小

                while ((len = in.read(buffer)) != -1) {
                    raf.write(buffer, 0, len);

                    total += len;
                    //[8]实现断点续传 就是把当前线程下载的位置 给存起来 下次再下载的时候 就是按照上次下载的位置继续下载 就可以了
                    int currentThreadPosition = startIndex + total;  //比如就存到一个普通的.txt文本中

                    //[9]用来存当前线程下载的位置
                    RandomAccessFile raff = new RandomAccessFile(fileStr + threadId + ".txt", "rwd");
                    raff.write(String.valueOf(currentThreadPosition).getBytes());
                    raff.close();

                }
                raf.close();//关闭流  释放资源

                System.out.println("线程id:" + threadId + "---下载完毕了");

                //[10]把.txt文件删除  每个线程具体什么时候下载完毕了 我们不知道
                synchronized (DownLoadThread.class) {
                    runningThread--;
                    if (runningThread == 0) {
                        //说明所有的线程都执行完毕了 就把.txt文件删除
                        for (int i = 0; i < runningThread; i++) {
                            File delteFile = new File(fileStr + i + ".txt");
                            delteFile.delete();
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-01-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档