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

Must-read tips for the top 10 Android market debuts in 2016!

Today, we bring you the top ten Android market la...

4 "user retention" methods used by Facebook!

When talking about user growth, we often talk abo...

NDRC releases electric vehicle electricity price policy: not expensive

The National Development and Reform Commission iss...

Case analysis of search promotion in local life service industry!

This article shares with you a search promotion c...

Unlock advanced methods of operating WeChat public accounts!

In addition to the conventional gameplay, what el...

The fifth anniversary of Juhe, a data carnival for developers

With the rapid development of mobile Internet, te...

New media operations: 1 formula and 6 principles to create new media content

This article will provide a systematic explanatio...

6 steps to promote products on Xiaohongshu!

With over 100 million monthly active users and 10...