这里有新鲜出炉的精品教程,程序狗速度看过来!
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
这篇文章主要为大家详细介绍了 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;
- }
- /**
- * 保存文件长度
- * @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);
- }
来源: http://www.phperz.com/article/17/0818/337615.html