多线程网络下载

如何使用多线程和流进行多线程网络下载

首先创建解析类



package com.m;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;

public class DownLoad2 {
	String path = "https://issuecdn.baidupcs.com/issue/netdisk/MACguanjia/BaiduNetdisk_mac_4.1.1.dmg";

	public void down() throws InterruptedException {
		String fileName="/Users/cheung/Desktop/down/baidu.dmg";
		try {
			URL url=new URL(path);
			System.out.println(url.getFile());
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(5 * 1000);
			conn.setRequestMethod("GET");
			int filelength = conn.getContentLength();
			System.out.println(filelength);
			RandomAccessFile file=new RandomAccessFile(fileName, "rw");
			file.setLength(filelength);
			file.close();
			conn.disconnect();
			int threadSize=3;
			int threadLength=filelength/3==0?filelength/3:filelength/3+1;
			for (int i = 0; i < threadSize; i++) {
				int startPostion=i*threadLength;
				RandomAccessFile rfile=new RandomAccessFile(fileName, "rw");
				rfile.seek(startPostion);
				new Thread(new DownLoad3(i, threadLength, startPostion, rfile, path)).start();
			}
			int quit = System.in.read(); 

			while ('q' !=quit){

				Thread.sleep(2*1000);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

创建线程下载



package com.m;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class DownLoad3 implements Runnable {
	private int threadDid;
	private int threadLength;
	private int startPostion;
	private RandomAccessFile rfile;
	private String path;
	
	public DownLoad3(int threadDid, int threadLength, int startPostion , RandomAccessFile rfile, String path) {
		super();
		this.threadDid = threadDid;
		this.threadLength = threadLength;
		this.startPostion = startPostion;
		this.rfile = rfile;
		this.path = path;
	}

	@Override
	public void run() {
		try {
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(5 * 1000);
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Range", "bytes=" + startPostion + "-");
			InputStream in = conn.getInputStream();
			byte[]b=new byte[1024];
			int len=-1;
			int length=0;
			while (length<threadLength && (len=in.read(b))!=-1) {
				rfile.write(b, 0, len);
				length+=len;
			}
			rfile.close();
			in.close();
			System.out.println("线程"+(threadDid+1)+"已经下载完毕");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("线程"+(threadDid+1)+"下载出错");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

创建测试类



package com.m;

public class Test {
	public static void main(String args[]) {
		DownLoad2 d = new DownLoad2();
		try {
			d.down();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
上一篇:python 实现web应用


下一篇:32_Go基础(TCP通信)