Android 之多线程下载原理

来源:互联网 发布:手机淘宝全屏海报尺寸 编辑:程序博客网 时间:2024/05/22 06:48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<pre name="code"class="java">packagecom.zengtao.tools;
 
importjava.io.InputStream;
importjava.io.RandomAccessFile;
importjava.net.HttpURLConnection;
importjava.net.URL;
 
publicclass DemoLoader {
    privatestatic DemoLoader loader = newDemoLoader();
    privatestatic int threadCount = 3;
 
    privateDemoLoader() {
 
    }
 
    // 单例设计模式
    publicstatic DemoLoader getInstance() {
        returnloader;
    }
 
    publicvoid downFile(String path) {
        // 去服务器端获取文件的长度,在本地创建一个跟服务器一样大小的文件
        try{
            URL url = newURL(path);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.setRequestMethod("GET");
            connection.setReadTimeout(5000);
            intcode = connection.getResponseCode();
            if(code == 200) {
                // 获取服务器端文件的长度
                intfileLength = connection.getContentLength();
                // 本地创建一个跟服务器一样大小的文件
                RandomAccessFile raf = newRandomAccessFile("setup.ext""rwd");
                raf.setLength(fileLength);
                raf.close();
                // 假设三个线程下载
                intblockSize = fileLength / threadCount;
                for(intthreadId = 0; threadId < threadCount; threadId++) {
                    intstartIndex = (threadId - 1) * blockSize;
                    intendIndex = threadId * blockSize - 1;
                    if(threadId == threadCount) {
                        endIndex = fileLength;
                    }
                    System.out.println("线程:"+ threadId + ",下载:"+ startIndex
                            "--->"+ endIndex);
                    // 开始下载
                    newDownLoadThread(threadId, startIndex, endIndex, path)
                            .start();
                }
                System.out.println("文件总长度为:"+ fileLength);
            else{
                System.out.println("请求失败!");
            }
 
        catch(Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 下载文件的主线程
     *
     * @author Administrator zengtao
     *
     */
    publicclass DownLoadThread extendsThread {
        privateint threadId;
        privateint startIndex;
        privateint endIndex;
        privateString path;
 
        /**
         *
         * @param threadId
         *            线程id
         * @param startIndex
         *            线程下载开始位置
         * @param endIndex
         *            线程下载结束位置
         * @param path
         *            线程下载结束文件放置地址
         */
        publicDownLoadThread(intthreadId, intstartIndex, intendIndex,
                String path) {
            super();
            this.threadId = threadId;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
            this.path = path;
        }
 
        @Override
        publicvoid run() {
            super.run();
            URL url;
            try{
                url = newURL(path);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                // 请求服务器下载部分的文件,制定开始的位置,和结束位置
                connection.setRequestProperty("Range""bytes="+ startIndex
                        "-"+ endIndex);
                connection.setDoInput(true);
                connection.setRequestMethod("GET");
                connection.setReadTimeout(5000);
                // 从服务器获取的全部数据,返回:200,从服务器获取部分数据,返回:206
                intcode = connection.getResponseCode();
                System.out.println("code = " + code);
                InputStream is = connection.getInputStream();
                RandomAccessFile raf = newRandomAccessFile("setup.exe""rwd");
                // 随机写文件的时候,从什么时候开始
                raf.seek(startIndex);
                intlen = 0;
                byte[] buff = newbyte[1024];
                while((len = is.read(buff)) != -1) {
                    raf.write(buff, 0, len);
                }
                is.close();
                raf.close();
                System.out.println("线程:"+ threadId + ",下载完成");
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}</pre>
原创粉丝点击