最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android HttpURLConnection斷點下載(單線程)

 更新時間:2017年05月27日 09:40:59   作者:銀伙計  
這篇文章主要為大家詳細介紹了Android HttpURLConnection斷點下載的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

HttpCilent 跟 HttpURLConnection 是安卓原生的用來實現(xiàn)http請求的類:
Android 6.0之后取消了HttpClient,不支持跟新 ,今天小編使用的是HttpURLConnection :

直接上代碼:

 URL url = null;
      BufferedInputStream bin = null;
      HttpURLConnection httpURLConnection = null;
      Context context;
      try {
      //你要下載文件的路徑
        String urlPath = "MyUrlPath"

        long fileSize = file.length;

        //獲取開始下載位置
        long startOffset = getFileLength(context);
        url = new URL(urlPath);
        //獲取HttpURLConnection對象
        httpURLConnection = (HttpURLConnection) url.openConnection();
        //設(shè)置請求方式
        httpURLConnection.setRequestMethod("GET");
        //設(shè)置字符編碼,這個字符編碼表示為頭500個字節(jié):Range: bytes=0-499
          表示第二個500字節(jié):Range: bytes=500-999
          表示最后500個字節(jié):Range: bytes=-500
          表示500字節(jié)以后的范圍:Range: bytes=500-
          第一個和最后一個字節(jié):Range: bytes=0-0,-1
          同時指定幾個范圍:Range: bytes=500-600,601-999
        httpURLConnection.setRequestProperty("Range" , "bytes=" + startOffset + "-");
        // 打開到此 URL 引用的資源的通信鏈接(如果尚未建立這樣的連接)。
        httpURLConnection.connect();
        if(httpURLConnection.getResponseCode() == 206){
         //if startOffset ==0 的時候,你就要把你的文件大小保存起來
          //獲取文件的大小httpURLConnection.getContentLength();
          //當你第一次下載的時候,也就是你的起始位置是0的時候,這就是這個文件的總大小,如果bytes=xx 的范圍大于0,那么你獲取的值就是你的文件總大小-bytes
          //獲取文件輸出流
          bin = new BufferedInputStream(httpURLConnection.getInputStream());
          //這個是你要保存在那個目錄的位置
          File folder= new File(DOWNLOADDIR);
          //如果文件夾不存在則新建一個文件夾
          if(!folder.exists()){
            folder.mkdirs();
          }

          // 隨機訪問文件,可以指定斷點續(xù)傳的起始位置
          //flieAbsolutePath 是你具體的文件路徑
          RandomAccessFile randomAccessFile = new RandomAccessFile(flieAbsolutePath , "rwd");
// rwd 跟 r 跟 w的區(qū)別是rwd:邊讀編寫邊下載 r讀 w寫          
          randomAccessFile.seek(startOffset);
          byte[] buffer = new byte[2048];
          int len;
          //isStop可以用來實現(xiàn)暫停功能
          while ((len = bin.read(buffer)) != -1 && !isStop) {
            randomAccessFile.write(buffer, 0, len);
            startOffset += len;
            //刷新下載進度
            Message msg = new Message();
            msg.what = (int)((startOffset * 100) / fileSize);
            //使用handler發(fā)送消息刷新UI
            handler.sendMessage(msg);
            //保存下載的位置到SharedPreferences,下次下載的時候拿值寫入設(shè)置字符編碼
            saveFileLength(context , startOffset);
          }
        }
      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }finally {
        if(url != null){
          url = null;

        }
        if(bin != null){
          try {
            bin.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
        if(httpURLConnection != null){
          httpURLConnection.disconnect();
        }

      }
      return null;
    }

/**
   * 保存文件長度
   * @param context
   * @param fileLength
   */
  private static void saveFileLength(Context context ,Long fileLength ){
    SharedPreferences sp = context.getSharedPreferences("My_SP" , Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putLong("File_startOffset" , fileLength);
    editor.commit();
  }
/**
   * 獲取文件長度
   * @param context
   * @return
   */
  private static Long getFileLength(Context context){
    SharedPreferences sp = context.getSharedPreferences("My_SP" , Context.MODE_PRIVATE);
    return sp.getLong("File_startOffset" , 0);
  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

柳州市| 民县| 安庆市| 衡东县| 上犹县| 宁南县| 正宁县| 台南市| 台北县| 赫章县| 丰顺县| 通城县| 武宁县| 沈阳市| 塘沽区| 南涧| 贡觉县| 澄江县| 普格县| 广水市| 寿阳县| 克拉玛依市| 石景山区| 张北县| 清涧县| 德安县| 安塞县| 忻城县| 土默特左旗| 崇仁县| 佛学| 濮阳市| 河西区| 潮安县| 海门市| 锡林浩特市| 环江| 南安市| 镇坪县| 霸州市| 龙海市|