如果要实现多线程下载,可以定义以下步骤:
1)创建URL对象(URL代表统一资源定位器)
URL url=new URL(path);//path资源路径
2)获取URL对象指向资源的大小,使用URLConnection类,该类代表应用程序和URL之间的通信链接。
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");//以GET方式获取
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
fileSize=conn.getContentLength();//得到文件大小
conn.disconnect();
3)在本地磁盘上创建一个与网络资源相同大小的空文件
RandomAccessFile files=new RandomAccessFile(targetFile, "rw");
files.setLength(fileSize);//设置文件大小
files.close();//关闭文件
4)创建线程,计算每个线程下载资源的大小并启动线程
代码如下:
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownUtil {
private String path;//下载地址
private String targetFile;//文件保存位置
private int threadNumber;//下载的线程数
private int fileSize;//文件总大小
private DownThread[] downThreads;//下载线程对象
public DownUtil(String path,String targetFile,int threadNumber){
this.path=path;
this.targetFile=targetFile;
this.threadNumber=threadNumber;
downThreads=new DownThread[threadNumber];//初始化数组
}
public void download() throws Exception{
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
fileSize=conn.getContentLength();//得到文件大小
conn.disconnect();
int currentPartSize=fileSize/threadNumber+1;//每线程对象下载文件的大小
RandomAccessFile files=new RandomAccessFile(targetFile, "rw");
files.setLength(fileSize);//设置文件大小
files.close();//关闭文件
for(int i=0;i<threadNumber;i++){
int start=i+currentPartSize;//每个线程下载的起始位置
RandomAccessFile file=new RandomAccessFile(targetFile, "rw");
file.seek(start);//定位下载位置
downThreads[i]=new DownThread(path,start,currentPartSize,file);
downThreads[i].start();//启动线程
}
}
}
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownThread extends Thread{
private int start;//下载线程起始位置
private int currentPartSize;//该线程下载文件的大小
private RandomAccessFile file;//存放的文件
private String path;//下载地址
private int length;//该线程已经下载的文件大小
public DownThread(String path,int start,int currentPartSize,RandomAccessFile file){
this.start=start;
this.currentPartSize=currentPartSize;
this.file=file;
this.path=path;
}
@Override
public void run() {
URL url;
try {
url = new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
InputStream in=conn.getInputStream();
in.skip(start);//跳过start个字节,每个线程只负责下载自己的文件
byte[] buffer=new byte[1024];
int hasRead=0;
while(length<currentPartSize&&(hasRead=in.read(buffer))!=-1){
file.write(buffer, 0, hasRead);
length+=hasRead;
}
file.close();
in.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/40783867 情绪控_