android 多线程下载原理

public class MultiThreadDown {
    static int ThreadCount = 4;
    
    static String path = "http://192.168.1.104:8080/android/pdf.exe";
    static long start = 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("m.mpg");
            // 生成临时文件
            RandomAccessFile raf = new RandomAccessFile(file, "rwd");
            // 设置临时文件的大小
            raf.setLength(length);
            raf.close();
            // 计算每个线程应该要下载多少个字节
            int size = length / ThreadCount;
            start = System.currentTimeMillis();
            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 {
            URL url = new URL(MultiThreadDown.path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(5000);
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            // 设置请求的数据的区间
            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("m.mpg");
                RandomAccessFile raf = new RandomAccessFile(file, "rwd");
                // 把文件的写入位置移动至startIndex
                raf.seek(startIndex);
                while ((len = is.read(b)) != -1) {
                    // 每次读取流里的数据写入临时文件
                    raf.write(b, 0, len);
                    total += len;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}  

你可能感兴趣的:(android 多线程下载原理)