您的位置:首页 > 理论基础 > 计算机网络

HttpUrlConnection实现多线程下载网络资源

2016-11-02 15:55 549 查看
/**
*
* @create_time:2016年11月1日  下午4:45:05
* @类描述:多线程下载工具类
* @version:
*
*/
public class DownUtil {
private String path;//下载资源的路径
private String targetFile;//下载文件的保存位置
private int threadNum;//线程数
private DownloadThread[] threads;
private int fileSize;//文件大小

public DownUtil(String path,String targetFile,int threadNum){
this.path = path;
this.targetFile = targetFile;
this.threadNum = threadNum;
threads = new DownloadThread[threadNum];//初始化threads数据
}
/**
*下载
*/
public void download() throws Exception{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5*1000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "image/gif,image/jpeg,image/pjpeg,application/x-shockwaveflash,application/xaml+xml,application/vnd.ms-xpsdocument,application/x-ms-xbap,application/x-ms-application,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE 7.0;Windows NT 5.2;Trident/4.0;.NET CLR 1.1.4322;.NET CLR 2.0.50727;.NET CLR 3.0.04506;.NET CLR 3.0.4506.2152;.NET CLR 3.5.30729)");
conn.setRequestProperty("Connection", "Keep-Alive");
fileSize = conn.getContentLength();//获取文件大小
conn.disconnect();
int currentPartSize = fileSize/threadNum +1;
RandomAccessFile file = new RandomAccessFile(targetFile, "rw");
file.setLength(fileSize);
file.close();
for (int i = 0; i < threadNum; i++) {
//每个线程下载的起始位置
int startPos = i*currentPartSize;
//每一个线程使用一个RandomAccessFile进行下载
RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");
//定位该线程的下载位置
currentPart.seek(startPos);
threads[i] = new DownloadThread(startPos, currentPartSize, currentPart);
threads[i].start();
}

}
//获取下载完成的百分比
public double getCompleteRate(){
int sumSize = 0;
for (int i = 0; i < threadNum; i++) {
sumSize +=threads[i].length;
}
return sumSize*1.0/fileSize;
}

private class DownloadThread extends Thread{
private int startPos;//当前线程的下载位置
private int currentPartSize;
//当前线程需要下在的文件块
private RandomAccessFile currentPart;
//定义已下载的字节数
public int length;

public DownloadThread(int startPos,int currentPartSize,RandomAccessFile currentPart){
this.startPos = startPos;
this.currentPart = currentPart;
this.currentPartSize = currentPartSize;
}
@Override
public void run() {
super.run();
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5*1000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "image/gif,image/jpeg,image/pjpeg,application/x-shockwaveflash,application/xaml+xml,application/vnd.ms-xpsdocument,application/x-ms-xbap,application/x-ms-application,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Charset", "UTF-8");
InputStream is = conn.getInputStream();
is.skip(this.startPos);
byte[] buffer = new byte[1024];
int hasRead = 0;
while (length<currentPartSize&& (hasRead = is.read(buffer))!=-1) {
currentPart.write(buffer);
length +=hasRead;
}
currentPart.close();
is.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: