Android 断点续传下载

断点续传下载,其实就是记录下原来下载的位置,然后从那个位置开始下载就是了。

代码如下。

  1. /**
  2.      * 作用:断点续传下载<BR>
  3.      * 作者:peijiangping<BR>
  4.      * 日期:2011-6-11
  5.      */ 
  6.     public staticint doDownloadTheFile_test(String strPath, String filePath, 
  7.             String filename, int mode,long size, Handler handler) { 
  8.         //file.size()即可得到原来下载文件的大小 
  9.         //下载路径 
  10.         String url = strPath; 
  11.         // 设置代理 
  12.         Header header = null
  13.         if (mode == 2) { 
  14.             //移动内网的时候使用代理 
  15.             url = format_CMWAP_URL(strPath); 
  16.             header = new BasicHeader("X-Online-Host"
  17.                     format_CMWAP_ServerName(strPath)); 
  18.         } 
  19.         HttpResponse response = null
  20.         // 用来获取下载文件的大小 
  21.         HttpResponse response_test = null
  22.         try
  23.             HttpClient client = new DefaultHttpClient(); 
  24.             HttpClient client_test = new DefaultHttpClient(); 
  25.             HttpGet request = new HttpGet(url); 
  26.             HttpGet request_test = new HttpGet(url); 
  27.             if (header !=null) { 
  28.                 request.addHeader(header); 
  29.             } 
  30.             response_test = client_test.execute(request_test); 
  31.             //获取需要下载文件的大小 
  32.             long fileSize = response_test.getEntity().getContentLength(); 
  33.             // 验证下载文件的完整性 
  34.             if (fileSize != 0 && fileSize == size) { 
  35.                 return 1
  36.             } 
  37.             //设置下载的数据位置XX字节到XX字节 
  38.             Header header_size = new BasicHeader("Range","bytes=" + size +"-" 
  39.                     + fileSize); 
  40.             request.addHeader(header_size); 
  41.             response = client.execute(request); 
  42. InputStream is = response.getEntity().getContent();
  43.             if (is == null) { 
  44.                 throw new RuntimeException("stream is null"); 
  45.             } 
  46.             SDCardUtil.createFolder(filePath); 
  47.             //获取文件对象,开始往文件里面写内容 
  48.             File myTempFile = new File(filePath +"/" + filename); 
  49.             RandomAccessFile fos = new RandomAccessFile(myTempFile,"rw"); 
  50.             //从文件的size以后的位置开始写入,其实也不用,直接往后写就可以。有时候多线程下载需要用 
  51.             fos.seek(size); 
  52.             byte buf[] = newbyte[1024]; 
  53.             long downloadfilesize =0
  54.             do
  55.                 int numread = is.read(buf); 
  56.                 if (numread <= 0) { 
  57.                     break
  58.                 } 
  59.                 fos.write(buf, 0, numread); 
  60.                 if (handler != null) { 
  61.                     Message msg = new Message(); 
  62.                     downloadfilesize += numread; 
  63.                     double percent = (double) (downloadfilesize + size) 
  64.                             / fileSize; 
  65.                     msg.obj = String.valueOf(percent); 
  66.                     handler.sendMessage(msg);// 更新下载进度百分比 
  67.                 } 
  68.             } while (true); 
  69.             is.close(); 
  70.         } catch (Exception ex) { 
  71.             ex.printStackTrace(); 
  72.             return -1
  73.         } 
  74.         return 1
  75.     } 
/**
	 * 作用:断点续传下载<BR>
	 * 作者:peijiangping<BR>
	 * 日期:2011-6-11
	 */
	public static int doDownloadTheFile_test(String strPath, String filePath,
			String filename, int mode, long size, Handler handler) {
		//file.size()即可得到原来下载文件的大小
		//下载路径
		String url = strPath;
		// 设置代理
		Header header = null;
		if (mode == 2) {
			//移动内网的时候使用代理
			url = format_CMWAP_URL(strPath);
			header = new BasicHeader("X-Online-Host",
					format_CMWAP_ServerName(strPath));
		}
		HttpResponse response = null;
		// 用来获取下载文件的大小
		HttpResponse response_test = null;
		try {
			HttpClient client = new DefaultHttpClient();
			HttpClient client_test = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpGet request_test = new HttpGet(url);
			if (header != null) {
				request.addHeader(header);
			}
			response_test = client_test.execute(request_test);
			//获取需要下载文件的大小
			long fileSize = response_test.getEntity().getContentLength();
			// 验证下载文件的完整性
			if (fileSize != 0 && fileSize == size) {
				return 1;
			}
			//设置下载的数据位置XX字节到XX字节
			Header header_size = new BasicHeader("Range", "bytes=" + size + "-"
					+ fileSize);
			request.addHeader(header_size);
			response = client.execute(request);
			InputStream is = response.getEntity().getContent();
			if (is == null) {
				throw new RuntimeException("stream is null");
			}
			SDCardUtil.createFolder(filePath);
			//获取文件对象,开始往文件里面写内容
			File myTempFile = new File(filePath + "/" + filename);
			RandomAccessFile fos = new RandomAccessFile(myTempFile, "rw");
			//从文件的size以后的位置开始写入,其实也不用,直接往后写就可以。有时候多线程下载需要用
			fos.seek(size);
			byte buf[] = new byte[1024];
			long downloadfilesize = 0;
			do {
				int numread = is.read(buf);
				if (numread <= 0) {
					break;
				}
				fos.write(buf, 0, numread);
				if (handler != null) {
					Message msg = new Message();
					downloadfilesize += numread;
					double percent = (double) (downloadfilesize + size)
							/ fileSize;
					msg.obj = String.valueOf(percent);
					handler.sendMessage(msg);// 更新下载进度百分比
				}
			} while (true);
			is.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
		return 1;
	}

调用:

  1. File file = new File(filepath); 
  2.                 long size = 0
  3.                 if (file.exists()) { 
  4.                     size = file.length(); 
  5.                 } 
  6.                 String downloadurl = "http://bs.baidu.com/appstore/apk_d9dba036522d0c2d71a77bc121b62d0b.apk"
  7.                 int value = HttpConnent.doDownloadTheFile_test(downloadurl, 
  8.                         Contants.sdCardPath + "/" + id, id + mode,1, size, 
  9.                         handler); 
  10.                 if (value == 1) { 
  11.                     System.out.println("下载OK"); 
  12.                 } else
  13.                     System.out.println("下载失败"); 
  14.                 } 
File file = new File(filepath);
				long size = 0;
				if (file.exists()) {
					size = file.length();
				}
				String downloadurl = "http://bs.baidu.com/appstore/apk_d9dba036522d0c2d71a77bc121b62d0b.apk";
				int value = HttpConnent.doDownloadTheFile_test(downloadurl,
						Contants.sdCardPath + "/" + id, id + mode, 1, size,
						handler);
				if (value == 1) {
					System.out.println("下载OK");
				} else {
					System.out.println("下载失败");
				}


转自:http://blog.csdn.net/peijiangping1989/article/details/7702807

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值