自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

android多線程斷點下載

移動開發(fā)
實現(xiàn)android 上面多線程斷點續(xù)傳下載 ,支持暫停,記憶任務(wù)。

源碼簡介

實現(xiàn)android 上面多線程斷點續(xù)傳下載 ,支持暫停,記憶任務(wù)。
源碼運行截圖

 

源碼片段

  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); // 設(shè)置客戶端可以接受的返回數(shù)據(jù)類型 
  8.                   
  9.                 http.setRequestProperty("Accept-Language""zh-CN"); 
  10.                 http.setRequestProperty("Referer", downUrl.toString());// 設(shè)置請求的來源,便于對訪問來源進行統(tǒng)計 
  11.                 http.setRequestProperty("Charset""UTF-8"); 
  12.   
  13.                 // 計算該線程下載的開始位置 
  14.                 int startPos = block * (threadId - 1) + downloadedLength; 
  15.                   
  16.                 /** 
  17.                  * 每塊結(jié)束的位置(***一塊需特殊處理,因為不能平均分配每塊大下,***一塊可能會有余數(shù)) 
  18.                  */ 
  19.                 //int endPosition = (i + 1) != threadCount ? ((i + 1) * blockSize - 1): fileSize; 
  20.                 // 計算該線程下載的結(jié)束位置 
  21.                 int endPos = threadId!=threadCount? (block *threadId):fileSize; 
  22.                 // 設(shè)置獲取實體數(shù)據(jù)的范圍,如果超過了實體數(shù)據(jù)的大小會自動返回實際的數(shù)據(jù)大小 
  23.                 http.setRequestProperty("Range""bytes=" + startPos + "-"+ endPos); 
  24.                 // 客戶端用戶代理 
  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.                 // 使用長連接 
  35.                 http.setRequestProperty("Connection""Keep-Alive"); 
  36.                 // 獲取遠程連接的輸入流 
  37.                 InputStream inStream = http.getInputStream(); 
  38.                 // 設(shè)置本地數(shù)據(jù)緩存的大小為1M 
  39.                 byte[] buffer = new byte[1024]; 
  40.                 // 設(shè)置每次讀取的數(shù)據(jù)量 
  41.                 int offset = 0
  42.                 // 打印該線程開始下載的位置 
  43.                 print("Thread " + this.threadId 
  44.                         + " starts to download from position " + startPos); 
  45.                 RandomAccessFile threadFile = new RandomAccessFile( 
  46.                         this.saveFile, "rwd"); 
  47.                 // 文件指針指向開始下載的位置 
  48.                 threadFile.seek(startPos); 

源碼鏈接:http://down.51cto.com/data/1980815

責(zé)任編輯:chenqingxiang 來源: 網(wǎng)絡(luò)整理
相關(guān)推薦

2011-05-31 13:29:40

Android 多線程

2012-12-25 11:39:20

Pythoncrawler

2014-12-31 15:42:21

Android多線程軟件下載

2009-03-12 10:52:43

Java線程多線程

2015-08-07 15:35:42

ios短點下載源碼

2017-04-17 19:31:03

Android多線程

2023-06-05 07:56:10

線程分配處理器

2023-06-06 08:17:52

多線程編程Thread類

2013-07-16 10:12:14

iOS多線程多線程概念多線程入門

2021-12-26 18:22:30

Java線程多線程

2010-01-21 11:27:30

linux多線程機制線程同步

2009-06-29 17:49:47

Java多線程

2020-12-28 08:03:26

多線程進程瀏覽器

2011-07-21 11:12:58

iPhone 線程 多線程

2023-06-13 13:39:00

多線程異步編程

2021-06-11 11:28:22

多線程fork單線程

2011-06-02 17:27:49

iphone 多線程

2024-05-29 09:13:37

2013-07-22 14:02:17

iOS開發(fā)ASIHTTPRequ

2023-06-07 13:49:00

多線程編程C#
點贊
收藏

51CTO技術(shù)棧公眾號