android 多线程下载断点续传

1.创建一个大小跟服务器文件相同大小的临时文件

2. 计算分配几个线程去下载服务器上的资源, 知道每个线程下载文件的位置

3. 开启多个线程,每个线程下载对应位置的文件

4.如果所有的线程,都把自己的数据下载完毕了,服务器上的资源就被下载到本地


	public static void main(String[] args) throws Exception {
		
		int threadcount = 3;
		
		String path = "http://127.0.0.1:8080/Kw.exe";
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setReadTimeout(5000);
		conn.setRequestMethod("GET");
		int code = conn.getResponseCode();
		if(code == 200){
			int length = conn.getContentLength();
			System.out.println("length:" + length);
			
			//在本地创建一个相应大小的临时文件
			RandomAccessFile raf = new RandomAccessFile("yd.exe", "rwd");
			raf.setLength(length);
			raf.close();
			
			int blockSize = length / threadcount; 
			for(int threadid = 1; threadid<=threadcount; threadid++){
				int startpositon = (threadid-1)*blockSize;
				int endposition =  threadid*blockSize - 1;
				if(threadid == threadcount){
					endposition = length ;
				}
				new ThreadDownLoad(length, startpositon, endposition, path).start();
				
				
				System.out.println("第" + threadid + "线程:" + startpositon + "----->" + endposition);
			}
			
		}else{
			System.out.println("请求失败");
		}
	}
	
	public static class ThreadDownLoad extends Thread{
		
		private int length;
		private int startposition;
		private int endposition;
		private String path;
		
		public ThreadDownLoad(int length, int startposition, int endposition,
				String path) {
			super();
			this.length = length;
			this.startposition = startposition;
			this.endposition = endposition;
			this.path = path;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				URL url = new URL(path);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				conn.setReadTimeout(5000);
				conn.setRequestMethod("GET");
				//重要:请求服务器下载部分的文件指定文件的位置
				conn.setRequestProperty("Range", "bytes=" + startposition + "-" + endposition);
				
				
				InputStream is = conn.getInputStream(); // 已经指定文件的位置,返回相应文件位置的输入流。
				RandomAccessFile raf = new RandomAccessFile("yd.exe", "rwd");
				//随机写文件的时候从哪个位置开始写
				raf.seek(startposition);
				int len = 0;
				byte[] buffer = new byte[1024];
				while( (len = is.read(buffer)) != -1){
					raf.write(buffer, 0, len);
				}
				is.close();
				raf.close();
				
				System.out.println(startposition + "----》" + endposition + "完成");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//super.run();
		}
	}
	





断点续传:


	public static void main(String[] args) throws Exception {
		
		int threadcount = 3;
		
		String path = "http://127.0.0.1:8080/ku.exe";
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setReadTimeout(5000);
		conn.setRequestMethod("GET");
		int code = conn.getResponseCode();
		if(code == 200){
			int length = conn.getContentLength();
			System.out.println("length:" + length);
			
			//在本地创建一个相应大小的临时文件
			RandomAccessFile raf = new RandomAccessFile("ku.exe", "rwd");
			raf.setLength(length);
			raf.close();
			
			int blockSize = length / threadcount; 
			for(int threadid = 1; threadid<=threadcount; threadid++){
				int startpositon = (threadid-1)*blockSize;
				int endposition =  threadid*blockSize - 1;
				if(threadid == threadcount){
					endposition = length ;
				}
				new ThreadDownLoad(threadid, startpositon, endposition, path).start();
				
				
				System.out.println("第" + threadid + "线程:" + startpositon + "----->" + endposition);
			}
			
		}else{
			System.out.println("请求失败");
		}
	}
	
	public static class ThreadDownLoad extends Thread{
		
		private int threadid;
		private int startposition;
		private int endposition;
		private String path;
		
		public ThreadDownLoad(int threadid, int startposition, int endposition,
				String path) {
			super();
			this.threadid = threadid;
			this.startposition = startposition;
			this.endposition = endposition;
			this.path = path;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				
				//检查文件是否有记录已经下载过的文件大小
				File tempFile = new File(threadid + ".txt");
				if(tempFile.exists() && tempFile.length()>0){
					FileInputStream fis = new FileInputStream(tempFile);
					byte[] temp = new byte[1024];
					int length = fis.read(temp);
					String downloadlength = new String(temp, 0, length);
					int downloadlengthint = Integer.parseInt(downloadlength);
					startposition = downloadlengthint;
				}
				
				
				
				URL url = new URL(path);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				conn.setReadTimeout(5000);
				conn.setRequestMethod("GET");
				//重要:请求服务器下载部分的文件指定文件的位置
				conn.setRequestProperty("Range", "bytes=" + startposition + "-" + endposition);
				
				
				InputStream is = conn.getInputStream(); // 已经指定文件的位置,返回相应文件位置的输入流。
				RandomAccessFile raf = new RandomAccessFile("ku.exe", "rwd");
				//随机写文件的时候从哪个位置开始写
				raf.seek(startposition);
				int len = 0;
				byte[] buffer = new byte[1024];
				//File file = new File(threadid + ".txt");
				int total = 0;
				while( (len = is.read(buffer)) != -1){
					// fos = new FileOutputStream(file); 
					RandomAccessFile file = new RandomAccessFile(threadid + ".txt", "rwd");
					raf.write(buffer, 0, len);
					total += len;
					file.write(String.valueOf(total + startposition + "").getBytes());
					file.close();
					//fos.write(String.valueOf(total).getBytes());
					//fos.flush();
					//fos.close();
					
				}
				is.close();
				raf.close();
				
				System.out.println(startposition + "----》" + endposition + "完成");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//super.run();
		}
	}
	








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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值