Java实现多线程下载工具类(网上下载百度图片)

本文介绍如何使用Java创建一个多线程下载工具类,特别适用于大文件的下载,例如从网上批量下载百度图片。
摘要由CSDN通过智能技术生成

DownUtil类 

package loading;

import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.omg.CORBA.portable.InputStream;

public class DownUtil {	
	//定义下载资源的路径
	private String path;
	//定义所下载文件的保存位置
	private String targetFile;
	//定义需要使用多少个线程下载资源
	private int threadNum;
	//定义下载的线程对象
	private DownThreads[] threads;
	//定义下载文件的总大小
	private int fileSize;
	public DownUtil(String path,String targetFile,int threadNum)
	{
		this.path=path;
		this.targetFile=targetFile;
		this.threadNum=threadNum;
		//初始化threads数组
		threads=new DownThreads[threadNum];
	}
	//定义下载函数
	public void download() throws Exception
	{
		URL url=new URL(path);
		HttpURLConnection conn=(HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		conn.setRequestProperty("Accept", 
				"image/gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值