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

"Swordsman" movie series three collections HD

The secret martial arts manual "Sunflower Ma...

Huanwang joins hands with Sofres to transform the smart TV industry with data

With the rapid popularization of smart TV termina...

Magical wormholes, more than just science fiction

Many people must have heard of "wormholes&qu...

The first lesson of money freedom: the "wallet" money management system

Is it difficult to make money? Saving money is ev...

NetQin takes off its safety helmet and builds a strong mobile business

April 10 (Reporter Lin Meibing) NetQin, which cla...

Product Operations: 10 Practical Strategies for Attracting New Users!

Nowadays, in an era where traffic is king, the co...

How much does it cost to develop a supplement mini program in Meishan?

How much does it cost to be an agent for a supple...

After the long Spring Festival holiday, how can we quickly get back to work?

The short Spring Festival holiday is coming to an...

Eight technologies that mobile developers cannot ignore

In recent years, the influence of programmers, es...