package org.act.down;import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;/*****************
* 多线程文件下载
* @author shenkunlin
* date 2013/8/13
*/
public class DownTest { /******************************
*
* @param path 请求地址
* @param threadnum 线程数量
*/
public void httpURL(String path){
try {
URL url =new URL(path);
HttpURLConnection con=(HttpURLConnection) url.openConnection();
//设置超时时间
con.setConnectTimeout(10*1000);
//设置请求方式
con.setRequestMethod("GET");
//获得文件的长度
int length =con.getContentLength();
//获取开启线程数量
int threadnum = threadCount(length);
//设置每个线程负责下载的文件大小
int size =(length % threadnum)==0 ? length/threadnum : length/threadnum+1;
//请求是否成功,处理下载
if(con.getResponseCode()==200){
//启用线程
ThreadPoolExecutor threadPool=new ThreadPoolExecutor(10, 100, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10), new ThreadPoolExecutor.DiscardOldestPolicy());
for (int i = 0; i < threadnum; i++) {
//处理链接,不处理会找不到资源
path=path.replaceAll("/", ""+File.separator+File.separator+"");
//开启线程下载文件
threadPool.execute(new ThreadDown(i, new File(filePath(), fileName(path)), size, url));
}
//等待线程执行完后终止所有线程
threadPool.shutdown();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/****************************
* 根据文件大小决定开启线程数量
* @param length 文件大小
* @return threadCount 开启线程数量
*/
public int threadCount(int length){
int size = (length/1024)/1024,threadCount=1;
//100MB以内 开启2个线程
if(size>=50 && size<=100){
threadCount=2;
}else if(size>100 && size<=400){
//400MB以内 开启3个线程
threadCount=3;
}else if(size>400 && size<=800){
//800MB以内 开启4个线程
threadCount=4;
}else if(size>800){
//大于800MB 开启5个线程
threadCount=5;
}
return threadCount;
}
/******************
* 文件路劲
* @return
*/
public String filePath(){
String filePath = "D:/download";
File file=new File(filePath);
if(!file.exists()){
file.mkdirs();
}
return filePath;
}
/******************************
* 获取文件名称
* @param path
* @return
*/
public String fileName(String path){
return path.substring(path.lastIndexOf(File.separator)+1);
}
}
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;/*****************
* 多线程文件下载
* @author shenkunlin
* date 2013/8/13
*/
public class DownTest { /******************************
*
* @param path 请求地址
* @param threadnum 线程数量
*/
public void httpURL(String path){
try {
URL url =new URL(path);
HttpURLConnection con=(HttpURLConnection) url.openConnection();
//设置超时时间
con.setConnectTimeout(10*1000);
//设置请求方式
con.setRequestMethod("GET");
//获得文件的长度
int length =con.getContentLength();
//获取开启线程数量
int threadnum = threadCount(length);
//设置每个线程负责下载的文件大小
int size =(length % threadnum)==0 ? length/threadnum : length/threadnum+1;
//请求是否成功,处理下载
if(con.getResponseCode()==200){
//启用线程
ThreadPoolExecutor threadPool=new ThreadPoolExecutor(10, 100, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10), new ThreadPoolExecutor.DiscardOldestPolicy());
for (int i = 0; i < threadnum; i++) {
//处理链接,不处理会找不到资源
path=path.replaceAll("/", ""+File.separator+File.separator+"");
//开启线程下载文件
threadPool.execute(new ThreadDown(i, new File(filePath(), fileName(path)), size, url));
}
//等待线程执行完后终止所有线程
threadPool.shutdown();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/****************************
* 根据文件大小决定开启线程数量
* @param length 文件大小
* @return threadCount 开启线程数量
*/
public int threadCount(int length){
int size = (length/1024)/1024,threadCount=1;
//100MB以内 开启2个线程
if(size>=50 && size<=100){
threadCount=2;
}else if(size>100 && size<=400){
//400MB以内 开启3个线程
threadCount=3;
}else if(size>400 && size<=800){
//800MB以内 开启4个线程
threadCount=4;
}else if(size>800){
//大于800MB 开启5个线程
threadCount=5;
}
return threadCount;
}
/******************
* 文件路劲
* @return
*/
public String filePath(){
String filePath = "D:/download";
File file=new File(filePath);
if(!file.exists()){
file.mkdirs();
}
return filePath;
}
/******************************
* 获取文件名称
* @param path
* @return
*/
public String fileName(String path){
return path.substring(path.lastIndexOf(File.separator)+1);
}
}