多线程网络下载

本文介绍如何利用Java进行多线程网络下载,通过创建解析类、线程下载及测试类来实现高效的文件下载流程。
摘要由CSDN通过智能技术生成

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

首先创建解析类



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();
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值