栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android HttpURLConnection断点下载(单线程)

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android HttpURLConnection断点下载(单线程)

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;
    }


  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();
  }

  private static Long getFileLength(Context context){
    SharedPreferences sp = context.getSharedPreferences("My_SP" , Context.MODE_PRIVATE);
    return sp.getLong("File_startOffset" , 0);
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158610.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号