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

The most comprehensive online and offline promotion guide for mini programs!

As mini programs become more and more sophisticat...

Fish that don't work hard will be punished by their leaders | Natural Trumpet

1) India suffers from high temperatures, nearly 4...

These functions of mobile phones are very practical but you have ignored them

More than a decade ago, it was still the era of fe...

How is the “Cross Tide” formed?

On July 30, a video of two tidal waves in the Qia...

4 dimensions, detailed explanation of refined operations!

The mobile Internet attaches more importance to t...

How to improve product conversion rate?

When we start promoting a product, we must first ...

Mutual Finance and Credit "High Cost-Effectiveness" User Growth Method (Routine)

The cost of acquiring customers in the Internet f...

From the streets to the Olympics, skateboarding should be seen!

Review expert: Peng Guoqiu, deputy chief physicia...

@All freshmen, how is your sun protection?

Military training is the beginning of the ivory t...