多线程下载示例代码

来源:互联网 发布:linux怎么上传文件zip 编辑:程序博客网 时间:2024/06/05 03:30


import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;


import android.os.Environment;


public class DownloadThread extends Thread {


private int startIndex;
private int endIndex;
private int threadId;
private String path;
private int runningThreadCount = 3;
public DownloadThread(int startIndex,int endIndex,int threadId,String path){
//开始位置  结束为止  线程标记  路径
this.startIndex = startIndex;
this.endIndex = endIndex;
this.threadId = threadId;
this.path = path;

}

public void run() {
try {
//多线程下载首先还是要建立连接
//首先还是要设置
URL url= new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
int code = conn.getResponseCode();
//206 表示请求一部分数据成功 
RandomAccessFile raf = new RandomAccessFile(Environment.getExternalStorageDirectory()+"/tomcat.png", "rw");
raf.seek(startIndex);
if(code == 206){
InputStream is = conn.getInputStream();

byte[] buffer = new byte[1024];
int len = -1;
while( (len = is.read(buffer)) != -1){
raf.write(buffer, 0, len);
}
is.close();
raf.close();
System.out.println("线程"+threadId+"现在完毕");


}else{
System.out.println("访问网络失败");
}

synchronized (this) {

runningThreadCount--;
if(runningThreadCount <= 0 ){
System.out.println("文件下载完毕........");
}



}

} catch (Exception e) {
e.printStackTrace();
}

};

}


这个地方就是我们根据文件的大小    将文件分为若干份大小     随后各部分分别创建子线程          进而实现我们的下载任务。




具体的使用:


private static int runningThreadCount = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

download();
}


private void download() {
      final String path = "http://192.168.1.106:8080/6.png";

new Thread(){
public void run() {
//锟竭程革拷锟斤拷
int threadCount = 3;
//锟竭筹拷锟斤拷锟截碉拷锟斤拷菘锟斤拷小
int blockSize =0;
try {
//1锟斤拷锟斤拷取锟斤拷锟斤拷锟斤拷锟斤拷锟侥硷拷锟侥达拷小
URL url= new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);

int code = conn.getResponseCode();
if(code == 200){
//锟斤拷取锟侥硷拷锟斤拷小
int length  = conn.getContentLength();
//锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷舜锟叫★拷锟酵拷目锟斤拷募锟�
RandomAccessFile raf = new RandomAccessFile(Environment.getExternalStorageDirectory()+"/tomcat.png", "rw");
raf.setLength(length);

int tempEndIndex ;
int tempStartIndex;

//锟斤拷锟斤拷blockSize
blockSize = length/threadCount;
for(int id=0; id<threadCount; id++){
//锟斤拷锟斤拷每锟斤拷锟竭筹拷锟斤拷锟斤拷锟斤拷莸锟斤拷锟绞嘉伙拷煤徒锟斤拷锟轿伙拷锟�
tempStartIndex = id * blockSize;
tempEndIndex = (id+1)*blockSize -1;
if(id == (threadCount - 1)){
tempEndIndex = length - 1;
}
final int startIndex = tempStartIndex;
final int endIndex = tempEndIndex;
final int threadId = id;
System.out.println("锟竭筹拷"+threadId+"锟斤拷锟截碉拷位锟矫o拷"+startIndex+"-"+endIndex);

  new DownloadThread( startIndex, endIndex, threadId, path).start();
}

}else{

}
} catch (Exception e) {
e.printStackTrace();
}

};
}.start();




}

}




注意  我们首先访问网络获得文件的大小。


进而分段进行下载 


我们将下载得到的文件存储在外存中。



















































































0 0