国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android HttpURLConnection斷點下載(單線程)

2019-10-23 18:27:44
字體:
來源:轉載
供稿:網友

HttpCilent 跟 HttpURLConnection 是安卓原生的用來實現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();        //設置請求方式        httpURLConnection.setRequestMethod("GET");        //設置字符編碼,這個字符編碼表示為頭500個字節:Range: bytes=0-499          表示第二個500字節:Range: bytes=500-999          表示最后500個字節:Range: bytes=-500          表示500字節以后的范圍:Range: bytes=500-          第一個和最后一個字節: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();          }          // 隨機訪問文件,可以指定斷點續傳的起始位置          //flieAbsolutePath 是你具體的文件路徑          RandomAccessFile randomAccessFile = new RandomAccessFile(flieAbsolutePath , "rwd");// rwd 跟 r 跟 w的區別是rwd:邊讀編寫邊下載 r讀 w寫                    randomAccessFile.seek(startOffset);          byte[] buffer = new byte[2048];          int len;          //isStop可以用來實現暫停功能          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發送消息刷新UI            handler.sendMessage(msg);            //保存下載的位置到SharedPreferences,下次下載的時候拿值寫入設置字符編碼            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);  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 崇信县| 青阳县| 南宫市| 大名县| 宝鸡市| 镇平县| 康平县| 德化县| 大关县| 乌恰县| 扎鲁特旗| 巫山县| 石景山区| 喀喇沁旗| 昌都县| 四子王旗| 酉阳| 卓尼县| 巫山县| 和顺县| 安西县| 兰西县| 福贡县| 光泽县| 杭锦旗| 双牌县| 平乡县| 夏津县| 西林县| 东平县| 合山市| 垫江县| 亚东县| 泸州市| 来凤县| 同德县| 大港区| 莆田市| 宕昌县| 武冈市| 斗六市|