android 断点续传功能实现

断点续传原理,其实是利用了临时文件保存了下载进度,当再次下载的时候,先读取临时文件里面的进度,再下载

public class MultiThreadDown {
    static int ThreadCount = 4;
    static String path = "http://192.168.1.103:8080/android/pdf.exe";
    static int finishedThread = 0;
    public static void main(String[] args) throws Exception {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(5000);
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        if (conn.getResponseCode() == 200) {
            // 1.先获取请求资源的大小
            int length = conn.getContentLength();
            File file = new File("pdf.exe");
            // 生成临时文件
            RandomAccessFile raf = new RandomAccessFile(file, "rwd");
            // 设置临时文件的大小
            raf.setLength(length);
            raf.close();
            // 计算每个线程应该要下载多少个字节
            int size = length / ThreadCount;
            for (int i = 0; i < ThreadCount; i++) {
                // 计算线程下载的开始位置和结束位置
                int startIndex = i * size;
                int endIndex = (i + 1) * size - 1;
                // 如果是最后一个线程,那么结束位置写死
                if (i == (ThreadCount - 1)) {
                    endIndex = length - 1;
                }
                new DownThread(startIndex, endIndex, i).start();
            }
        }
    }
}
class DownThread extends Thread {
    int startIndex;
    int endIndex;
    int threadId;
    public DownThread(int startIndex, int endIndex, int threadId) {
        super();
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.threadId = threadId;
    }
    @Override
    public void run() {
        // 再次发送HTTP请求,下载源文件
        try {
            // 生成一个专门用来记录下载进度的临时文件
            File progressFile = new File(threadId + ".txt");
            if (progressFile.exists()) {
                FileInputStream fis = new FileInputStream(progressFile);
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        fis));
                // 从进度临时文件中读取出上一次下载的总进度,然后与原本的开始位置相加,得到新的开始位置
                startIndex += Integer.parseInt(br.readLine());
                fis.close();
            }
            URL url = new URL(MultiThreadDown.path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(5000);
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            System.out.println("线程threadId" + threadId + "下载区间:" + startIndex
                    + "-" + endIndex);
            // 设置请求的数据的区间
            conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
                    + endIndex);
            // 请求部分数据,响应码为206
            if (conn.getResponseCode() == 206) {
                // 此时只有 1/threadcount数据
                InputStream is = conn.getInputStream();
                byte[] b = new byte[1024];
                int len = 0;
                long total = 0;
                File file = new File("pdf.exe");
                // 这样可以保证数据同步写入硬盘中,防止停电等原因da
                RandomAccessFile raf = new RandomAccessFile(file, "rwd");
                // 把文件的写入位置移动至startIndex
                raf.seek(startIndex);
                while ((len = is.read(b)) != -1) {
                    // 每次读取流里的数据写入临时文件
                    raf.write(b, 0, len);
                    total += len;
                    RandomAccessFile progressRaf = new RandomAccessFile(
                            progressFile, "rwd");
                    progressRaf.write((total + "").getBytes());
                    progressRaf.close();
                }
                raf.close();
                progressFile.delete(); // 下载完成后,将临时文件删除
                ++MultiThreadDown.finishedThread;
                synchronized (MultiThreadDown.path) {
                    if (MultiThreadDown.finishedThread == MultiThreadDown.ThreadCount) {
                        for (int i = 0; i < MultiThreadDown.ThreadCount; i++) {
                            File f = new File(i + ".txt");
                            f.delete();
                        }
                        MultiThreadDown.finishedThread = 0;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}  

你可能感兴趣的:(android 断点续传功能实现)