[置顶] java版多线程下载

只是记录一下,java多线程下载,没有断点续传,

其实可以加,只是今天没有精力了,

 

思路就是,每次下载一段之后保存下进度,下次启动的时候读一下进度就可以了。

 

package com.hung.main;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

public class Main {

	private static boolean ISDEBUG = false;
	private static int ThreadCound = 3;

	public static void main(String[] args) throws ClientProtocolException,
			IOException {
		String path = "http://192.168.1.162:8081/test.exe";

		HttpClient hc = HttpClientBuilder.create().build();
		HttpGet hg = new HttpGet(path);
		HttpResponse hr = hc.execute(hg);
		int stauts = hr.getStatusLine().getStatusCode();
		if (200 != stauts) {
			System.out.println("请求失败,状态码:" + stauts);
			return;
		}

		long length = hr.getEntity().getContentLength();
		RandomAccessFile raf = new RandomAccessFile("test.exe", "rwd");
		raf.setLength(length);

		raf.close();

		long blockSize = length / ThreadCound;

		System.out.println("总长度:" + length + " 块长度: " + blockSize);

		for (int i = 1; i <= ThreadCound; i++) {
			long starIndex = (i - 1) * blockSize;
			long endIndex = i * blockSize - 1;
			if (i == ThreadCound) {
				// 从0开始,所以结尾应该是总长度 -1
				endIndex = length - 1;
			}
			new MyThread(path, starIndex, endIndex).start();

		}

	}

	private static class MyThread extends Thread {
		private String url;
		private long start;
		private long end;
		private String threadName;

		public MyThread(String url, long start, long end) {
			this.url = url;
			this.start = start;
			this.end = end;
			if(ISDEBUG){
				System.out.println("下载范围:" + start + " ~ " + end);
			}
		}

		@Override
		public void run() {
			threadName = Thread.currentThread().getName();
			try {
				RandomAccessFile raf = new RandomAccessFile("test.exe", "rwd");

				HttpClient hc = HttpClientBuilder.create().build();
				HttpGet hg = new HttpGet(url);
				hg.setHeader("Range", "bytes=" + start + "-" + end);
				HttpResponse hr = hc.execute(hg);

				int status = hr.getStatusLine().getStatusCode();
				if (status < 200 || 299 < status) {
					System.out.println(threadName + " 请求失败");
					return;
				}
				if (ISDEBUG) {
					String s1 = threadName + "状态码:" + status;
					System.out.println(s1);
					long l = hr.getEntity().getContentLength();
					String s2 = threadName + "内容长度:" + l;
					System.out.println(s2);
				}
				InputStream is = hr.getEntity().getContent();
				raf.seek(start);
				int i = 0;
				long total = 0;
				byte[] bs = new byte[1024 * 8];
				while ((i = is.read(bs)) != -1) {
					raf.write(bs, 0, i);
					total += i;
					// System.out.println(Thread.currentThread().getName()
					// +" 下载进度 : " + total);
				}

				raf.close();
				is.close();
				System.out.println(threadName + " 下载完成");
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值