Project Source Download
Https://github.com/Wang-Jun-Chao/AndroidProjects
Multi-Threaded Download
Principle: Server CPU allocated to each thread of the same time slice, the server bandwidth evenly allocated to each thread, so the more threads the client opens, you can preempt more server resources
Determine how much data each thread downloads
Send HTTP request to download address
String path =
url url = new URL (path)
httpurlconnection conn = (httpurlconnection) URL ()
conn ()
Conn () C8/>conn ()
Gets the total length of the file, and then creates a consistent, temporary file
(Conn.getresponsecode () = =) {
length = Conn.getcontentlength ();
Randomaccessfile RAF = randomaccessfile (getfilename (path));
Raf.setlength (length);
Raf. ();
Determine how much data the thread downloads
BlockSize = /thread_count;
Calculates the starting and ending positions of each thread's downloaded data
( = ; <=; + +) {
StartIndex = (-) * blockSize;
Endindex = * blockSize-;
(= = Thread_count) {
endindex = length;
}
New Downloadthread (StartIndex, Endindex,) ();
}
Send the request to the download address again, the request start position to the end position data
String path =;
URL url = url (path);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setreadtimeout);
Conn.setconnecttimeout);
Conn.setrequestmethod ();
Conn.setrequestproperty (, + startIndex + + endindex);
Conn.connect ();
* Download the requested data and store it in the temporary file
(conn.getresponsecode () = =) {
InputStream is = Conn.getinputstream ();
Randomaccessfile RAF = randomaccessfile (getfilename (path));
Raf.seek (startIndex);
[] b = ];
;
while (= Is.read (b))!=) {
raf.write (b,,);
}
Raf. ();
}
Multi-threaded Download with breakpoint continuation
Define an int variable to record the total length of the data downloaded by each thread, then add the thread's download start position, and the result is the start of the thread at the next download, and the resulting result is stored in the cached file.
Total =;
while (= Is.read (b))!=) {
raf.write (b,,);
Total + =;
Randomaccessfile raf2 = randomaccessfile (threadId +,);
Raf2.write ((StartIndex + total +). GetBytes ());
Raf2. ();
}
The next time the download starts, read the value in the cached file, and the resulting value is the starting position of the line threading.
FileInputStream FIS = fileinputstream ();
BufferedReader br = bufferedreader (inputstreamreader (FIS));
Text = Br.readline ();
int newstartindex =. parseint (text);
Read the value as a new starting position
StartIndex = Newstartindex;
Fis.close ();
After all three threads have been downloaded, delete the cached file
running_thread--;
(Running_thread = =) {
(i =; I <=; i++) {
file F = file (i +);
F. ();
}
Mobile version of the breakpoint continued to pass the multithread download device
Just paste the code directly over it, and remember to change the path to the Android directory when accessing the file, adding access to the network and external storage
Show download progress with progress bar
When you get the total length of the download file, set the maximum value of the progress bar
Pb.setmax ();
The progress bar needs to display three threads of the overall download progress, so three threads per download, the new download length will be added to the progress bar
Defines an int global variable that records the total download length of three threads
Progress
* Refresh progress bar
while (= Is.read (b))!=) {
raf.write (b,,);
Progress + =;
Pb.setprogress (progress);
Every time the breakpoint is downloaded, the new start is downloaded, and the progress bar is displayed from the new location, and the progress bar progress is also processed when the cache file is fetched for the newly downloaded start position.
FileInputStream FIS = fileinputstream (file);
BufferedReader br = bufferedreader (inputstreamreader (FIS));
String text = Br.readline ();
Newstartindex = Integer.parseint (text);
Alreadydownload = Newstartindex-startindex;
Progress + = Alreadydownload;
Add a text box to show percent progress
TV (Progress * /PB () +)
The use of Httputils
Httputils itself supports multi-threaded breakpoint continuation, it is very convenient to use
Creating Httputils Objects
=
Download files
Http.download (URL,
target,,,
requestcallback<file> () {
responseinfo<file> arg0) {
Tv.settext (+ Arg0.result.getPath ());
}
(HttpException arg0, String arg1) {
Tv.settext (+ arg1);
}
(Total, current , isuploading) {
. onloading (Total, Current, isuploading);
Pb.setmax (() total);
Pb.setprogress (() current);
Tv_progress.settext (Current * /Total +);
}
});
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/