Android multi-threaded breakpoint download

Android multi-threaded breakpoint download

Source code introduction

Implement multi-threaded breakpoint-resume download on Android, support pause and remember tasks.
Source code running screenshot

Source code snippet

  1. try {
  2. HttpURLConnection http = (HttpURLConnection) downUrl
  3. .openConnection();
  4. http.setConnectTimeout( 5 * 1000 );
  5. http.setRequestMethod( "GET" );
  6. http.setRequestProperty( "Accept" , "image/gif, image/jpeg, image/pjpeg, image/pjpeg,application/x-shockwave-flash, application/xaml+xml,application/vnd.ms-xpsdocument, application/x-ms-xbap,application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword" );
  7. // http.setRequestProperty(, null); // Set the return data type that the client can accept  
  8.                   
  9. http.setRequestProperty( "Accept-Language" , "zh-CN" );
  10. http.setRequestProperty( "Referer" , downUrl.toString()); // Set the source of the request to facilitate statistics on access sources  
  11. http.setRequestProperty( "Charset" , "UTF-8" );
  12.   
  13. // Calculate the start position of the download thread  
  14. int startPos = block * (threadId - 1 ) + downloadedLength;
  15.                   
  16. /**
  17. * The end position of each block (the last block needs special treatment, because each block cannot be evenly distributed, and the last block may have a remainder)
  18. */  
  19. //int endPosition = (i + 1) != threadCount ? ((i + 1) * blockSize - 1): fileSize;  
  20. // Calculate the end position of the thread download  
  21. int endPos = threadId!=threadCount? (block *threadId):fileSize;
  22. // Set the range of entity data to be obtained. If the size of the entity data is exceeded, the actual data size will be automatically returned.  
  23. http.setRequestProperty( "Range" , "bytes=" + startPos + "-" + endPos);
  24. // Client user agent  
  25. http.setRequestProperty( "User-Agent" ,
  26. "Mozilla/4.0 (compatible; MSIE 8.0;"  
  27. + "Windows NT 5.2; Trident/4.0;"  
  28. + ".NET CLR 1.1.4322;"  
  29. + ".NET CLR 2.0.50727;"  
  30. + ".NET CLR 3.0.04506.30;"  
  31. + ".NET CLR 3.0.4506.2152;"  
  32. + " .NET CLR 3.5.30729)" );
  33.   
  34. // Use long connection  
  35. http.setRequestProperty( "Connection" , "Keep-Alive" );
  36. // Get the input stream of the remote connection  
  37. InputStream inStream = http.getInputStream();
  38. // Set the size of the local data cache to 1M  
  39. byte [] buffer = new   byte [ 1024 ];
  40. // Set the amount of data to read each time  
  41. int offset = 0 ;
  42. // Print the location where the thread starts downloading  
  43. print( "Thread " + this .threadId
  44. + " starts to download from position " + startPos);
  45. RandomAccessFile threadFile = new RandomAccessFile(
  46. this .saveFile, "rwd" );
  47. // The file pointer points to the location where the download starts  
  48. threadFile.seek(startPos);

Source code link: http://download..com/data/1980815

<<:  Android application source code captures global exceptions

>>:  Xinyou Internet CEO: Creating an ideal connector for multi-screen entertainment

Recommend

Why is Amazon so late in launching a smartphone?

From the product comparison, Amazon's tablet w...

iOS 9 public beta launches smart prediction/power saving mode

[[139793]] In the early morning of July 10th, Bei...

Take 3 minutes to determine whether your breasts are healthy?

appendix: Here’s another little piece of knowledg...

Japan develops microrobots: DNA controls movement like cells

Living things are the most inspiring, but also di...

Young people, are your health-preserving methods reliable?

On Douban, a group called "We Love Health Pr...

How to improve product activity? There are these 6 operating routines

In fact, I haven’t used QQ to chat for a long tim...

The Hunger Games of Internet TV: LeTV’s Suspension

Once any business model touches upon policy restr...

The "Giant Panda" in the farmland: We finally know how old it is!

Produced by: Science Popularization China Author:...

How much does it cost to develop a luggage mini program in Alar?

WeChat Mini Program is an application that users ...

How to increase App users by not filling in invitation codes

What should we do when fission marketing becomes ...