使用Java方法实现多线程下载案例

来源:互联网 发布:域名注册购买 编辑:程序博客网 时间:2024/05/16 04:05

 使用Java方法实现多线程下载案例

 使用tomcat 内部调用下载某个.exe应用。


package com.itheima.Utils;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream.GetField;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class MuiltDownload {
//1.设置下载地址
static String path ="http://192.168.10.2:8080/epp_4.3.1256.0.exe";
public static void main(String[] args) {
int ThreadCount =3; 




try {
//2.设置下载地址
          URL url = new URL(path);
    //3.设置读取
  HttpURLConnection conn =(HttpURLConnection) url.openConnection();
//4.设置提交方式
   conn.setRequestMethod("GET");
   //设置响应和返回时长
   conn.setReadTimeout(5000);
   conn.setConnectTimeout(5000);
   //5.设置建立连接
   conn.connect();
   //6.判断当前状态
   if(conn.getResponseCode() ==200){
    //7.拿到请求资源的长度
    int length = conn.getContentLength();
    //8.设置存放位置
    File file = new File(getFileName(path));
    //设置占位符
    RandomAccessFile raf = new RandomAccessFile(file, "rwd");
    //设置临时文件的大小
    raf.setLength(length);
    raf.close();
    //9.获取每个线程读取的长度
    int size = length / ThreadCount;
    for(int i =0;i<ThreadCount ;i++){
    int startIndex = i * size;
    int endIndex =(i +1) * size -1;
    //如果是最后一个线程,那么久结束位置执行
    if(i == ThreadCount -1){
    endIndex = length -1;
    }
    System.out.println("线程"+i+"执行区间"+startIndex+"后续"+endIndex);
    new DownlodThread(startIndex,endIndex,i).start();
    }
   }
   
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}



public static String getFileName(String path){
int idenx = path.lastIndexOf("/");
return path.substring(idenx+1);
}





}
class DownlodThread extends Thread{
int startIndex;
int endIndex;
int threadId;
public DownlodThread(int startIndex, int endIndex, int threadId) {
super();
this.startIndex = startIndex;
this.endIndex = endIndex;
this.threadId = threadId;
}


@Override
public void run() {
// TODO Auto-generated method stub
super.run();
URL url;
try {
 url = new URL(MuiltDownload.path);
//3.设置读取
  HttpURLConnection conn =(HttpURLConnection) url.openConnection();
//4.设置提交方式
   conn.setRequestMethod("GET");
   //设置响应和返回时长
   conn.setReadTimeout(5000);
   conn.setConnectTimeout(5000);
   //设置本次Http请求的区间
   conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
   //判断当前返回状态码
   if(conn.getResponseCode() ==206){
    InputStream is = conn.getInputStream();
    byte[] b = new byte[1024];
    int len =0;
    int total =0;
    File file = new File(MuiltDownload.getFileName(MuiltDownload.path));


    //拿临时文件应用
    RandomAccessFile raf = new RandomAccessFile(file, "rwd");
    //从每个节点进行读取
    raf.seek(startIndex);
    while((len =is.read(b))!=-1){
    raf.write(b, 0, len);
    total +=len;
    System.out.println("当前线程"+threadId +"下载:"+total);
    }
    raf.close();
   }
   
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}
原创粉丝点击