JAVA线程实现下载_用java实现简单的多线程下载

public class DownloadNetTest {

private File fileOut;

private URL url;

private long fileLength=0;//初始化线程数   private int ThreadNum=5;

public DownloadNetTest(){

try{

System.out.println("正在链接URL");

url=new URL("http://211.64.201.201/uploadfile/nyz.mp3");

HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();

fileLength=urlcon.getContentLength();

if(urlcon.getResponseCode()>=400){

System.out.println("服务器响应错误");

System.exit(-1);

}    if(fileLength<=0)

System.out.println("无法获知文件大小");

//打印信息

printMIME(urlcon);

System.out.println("文件大小为"+fileLength/1024+"K");

//获取文件名

String trueurl=urlcon.getURL().toString();

String filename=trueurl.substring(trueurl.lastIndexOf('/')+1);

fileOut=new File("D://",filename);

}

catch(MalformedURLException e){

System.err.println(e);

}

catch(IOException e){

System.err.println(e);

}

init();

}

private void init(){

DownloadNetThread [] down=new DownloadNetThread[ThreadNum];

try {

for(int i=0;i

RandomAccessFile randOut=new RandomAccessFile(fileOut,"rw");

randOut.setLength(fileLength);

long block=fileLength/ThreadNum+1;

randOut.seek(block*i);         down[i]=new DownloadNetThread(url,randOut,block,i+1);

down[i].setPriority(7);

down[i].start();

}

//循环判断是否下载完毕

boolean flag=true;

while (flag) {

Thread.sleep(500);

flag = false;

for (int i = 0; i < ThreadNum; i++)

if (!down[i].isFinished()) {

flag = true;

break;

}

}// end while

System.out.println("文件下载完毕,保存在"+fileOut.getPath() );

} catch (FileNotFoundException e) {

System.err.println(e);

e.printStackTrace();

}

catch(IOException e){

System.err.println(e);

e.printStackTrace();  }

catch (InterruptedException e) {

System.err.println(e);

}

}

private void printMIME(HttpURLConnection http){

for(int i=0;;i++){

String mine=http.getHeaderField(i);

if(mine==null)

return;

System.out.println(http.getHeaderFieldKey(i)+":"+mine);

}

}

public static void main(String[] args) {

DownloadNetTest app=new DownloadNetTest();

}

}

//线程类public class DownloadNetThread extends Thread{ private InputStream randIn; private RandomAccessFile randOut; private URL url; private long block; private int threadId=-1; private boolean done=false;    public DownloadNetThread(URL url,RandomAccessFile out,long block,int threadId){    this.url=url;    this.randOut=out;    this.block=block;    this.threadId=threadId; }  public void run(){    try{       HttpURLConnection http=(HttpURLConnection)url.openConnection();       http.setRequestProperty("Range","bytes="+block*(threadId-1)+"-");       randIn=http.getInputStream();    }    catch(IOException e){       System.err.println(e);    }       byte [] buffer=new byte[1024];    int offset=0;    long localSize=0;    System.out.println("线程"+threadId+"开始下载");    try {       while ((offset = randIn.read(buffer)) != -1&&localSize<=block) {       randOut.write(buffer,0,offset);       localSize+=offset;       }       randOut.close();       randIn.close();       done=true;       System.out.println("线程"+threadId+"完成下载");       this.interrupt();    }    catch(Exception e){       System.err.println(e);    } }   public boolean isFinished(){      return done;   } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值