找回密码
 立即注册
线程下载thedownload | android开发 2022-05-21 108 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
Android应用源码Android平台下通过HTTP协议实现断点续传下载
我们正在为 Andorid 的 HTTP 协议编写一个多线程断点下载应用程序。直接使用单线程下载HTTP文件对我们来说是一件非常简单的事情。那么,多线程断点需要什么功能呢?

1.多线程下载,

2.支持断点。



使用多线程的好处:使用多线程下载会提高文件下载的速度。多线程下载文件的过程是:

(1)先获取下载文件的长度,再设置本地文件的长度。


   HttpURLConnection.getContentLength();//获取下载文件的长度

  RandomAccessFile file = new RandomAccessFile(“QQWubiSetup.exe”,“rwd”);

    file.setLength(filesize);//设置本地文件的长度



(2)根据文件长度和线程数计算每个线程下载的数据长度和下载位置。

   例如:文件长度为6M,线程数为3,那么每个线程下载的数据长度为2M,





比如10M大小,使用3个线程下载,

    线程下载的数据长度(10%3 == 0?10/3:10/3+1),第一、二线程的下载长度为4M,第三线程的下载长度为2M

     下载起始位置:线程id * 每个线程下载的数据长度 = ?

    下载结束位置:(线程id+1)*每个线程下载的数据长度-1=?



(3) 使用Http的Range头域来指定每个线程从哪里开始从文件下载,从哪里下载。

     例如:指定从文件的2M位置开始下载,下载到该位置(4M-1byte)

           代码如下: HttpURLConnection.setRequestProperty("Range", "bytes=2097152-4194303");



(4) 保存文件并使用 RandomAccessFile 类指定每个线程从本地文件开始写入数据的位置。

RandomAccessFile threadfile = new RandomAccessFile("QQWubiSetup

(Android application source code realizes breakpoint continuous download through HTTP protocol on Android platform
We are writing a multithreaded breakpoint download application for andorid's HTTP protocol. Downloading HTTP files directly using single thread is a very simple thing for us. So, what functions do multithreaded breakpoints need?
1. Multi thread download,
2. Support breakpoints.
Benefits of using multithreading: using multithreaded download will improve the speed of file download. The process of downloading files through multiple threads is as follows:
(1) First obtain the length of the downloaded file, and then set the length of the local file.
HttpURLConnection. getContentLength();// Gets the length of the downloaded file
RandomAccessFile file = new RandomAccessFile(“QQWubiSetup.exe”,“rwd”);
file. setLength(filesize);// Set the length of the local file
(2) Calculate the data length and download location of each thread according to the file length and the number of threads.
For example, if the file length is 6m and the number of threads is 3, the data downloaded by each thread is 2m,
For example, 10m size, Download with three threads,
The data length downloaded by the thread (10% 3 = = 0 × 10 / 3:10 / 3 + 1), the download length of the first and second threads is 4m, and the download length of the third thread is 2m
Download start position: thread ID * data length downloaded by each thread =?
Download end location: (thread ID + 1) * data length downloaded by each thread - 1 =?
(3) Use the range header field of HTTP to specify where each thread starts downloading from files and where to download from.
For example: specify to download from 2m location of the file and download to this location (4m-1byte)
The code is as follows: httpurlconnection setRequestProperty("Range", "bytes=2097152-4194303");
(4) Save the file and use the RandomAccessFile class to specify where each thread writes data from the local file.
RandomAccessFile threadfile = new RandomAccessFile("QQWubiSetup)

[下载]13321618059.rar




上一篇:Android应用源码模仿zaker风景页面滑动效果修改版
下一篇:Android应用源码Hibernate4Android