码迷,mamicode.com
首页 > 编程语言 > 详细

多线程下载图片

时间:2018-06-27 11:33:07      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:extend   final   在服务器   put   rac   下载图片   public   static   nts   

package mutiDownload;

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

/**
* 多线程下载文件
* @author Administrator
*
*/
public class Demo {


public final static int threadcount=3;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

//获取服务器上的文件长度,在本地创建一个和服务器上的文件一样大的临时文件
String pathString="http://192.168.1.84:8091/androidServer/test.mkv";
URL url=new URL(pathString);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
int code=connection.getResponseCode();
if(code==200){ //请求成功
int length=connection.getContentLength();
System.out.println("文件长度:"+length);
RandomAccessFile ref=new RandomAccessFile("test.mkv","rwd");
//指定创建的临时文件的长度
ref.setLength(length);
ref.close();

int blocksize=length/threadcount;
for(int threadId=1;threadId<=threadcount;threadId++){
int startIndex=(threadId-1)*blocksize; //线程开始位置
int endIndex=threadId*blocksize-1;
if(threadId==threadcount){
endIndex=length;
}
System.out.println("线程"+threadId+"下载---"+startIndex+"-->"+endIndex);
new Downhread(threadId, startIndex, endIndex, pathString).start();;

}
}else{
System.out.println("服务器错误");
}

}

/**
* 下载文件子线程
* @author Administrator
*
*/
public static class Downhread extends Thread {
private int threadId;
private int startIndex;
private int endIndex;
private String path;



/**
*
* @param threadId 线程id
* @param startIndex 线程下载的开始位置
* @param endIndex
* @param path 下载文件在服务器上的位置
*/
public Downhread(int threadId, int startIndex, int endIndex, String path) {
super();
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.path = path;
}




@Override
public void run() {
// TODO Auto-generated method stub
//super.run();
try {
URL url=new URL(path);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
connection.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
int code=connection.getResponseCode();
System.out.println("code:"+code);
// if(code==200){ //请求成功
//
// }
InputStream is=connection.getInputStream();
RandomAccessFile raf=new RandomAccessFile("test.mkv","rwd");
//写文件的时候指定从哪个位置开始写
raf.seek(startIndex);

int len=0;
byte [] buffer=new byte[1024];
while((len=is.read(buffer))!=-1){
raf.write(buffer, 0, len);

}
is.close();
raf.close();
System.out.println("线程:"+threadId+"下载完毕了...");

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

多线程下载图片

标签:extend   final   在服务器   put   rac   下载图片   public   static   nts   

原文地址:https://www.cnblogs.com/wangc04/p/9232870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!