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

5 Lessons a 19-Year-Old Programmer Learned at Google

As a naive, enthusiastic 19-year-old, I walked in...

Is it necessarily the result of staying up late if you have “panda eyes”?

Key Points ★ Staying up late does affect dark cir...

About iOS Private API Scanning

Introduction: I recently studied the topic of pri...

QR code generation and scanning source code download

The QR code generation and scanning function code...

Two dimensions teach you how to spread high-quality content?

In an era where content is king, the value of hig...

10 ways to attract new customers and boost your traffic

What I’m going to share today is how to use socia...

Event planning and promotion: a universal event planning solution!

There is actually no shortcut to planning an even...

Baidu search promotion optimization detailed explanation!

Today, I will share with you the introductory kno...