多线程下载的写法,java版本

来源:互联网 发布:软件出现乱码怎么办 编辑:程序博客网 时间:2024/06/08 19:12

注意事项:我写ZZZ的地方不是正规的代码写法,是因为自我检测的需要。



public class Multidload {
      static int ThreadCount = 3;
      //确定下载地址
      static String path = "http://192.168.6.125:8080/TakeColor.exe";

      public static void main(String[] args){

      //发送get请求,请求这个资源的地址
      try {
        URL url = new URL(path);
        HttpURLConnection conn =(HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(5000);

        if(conn.getResponseCode()==200){
            //拿到所请求文件的大小
        int length = conn.getContentLength();
        File file = new File("TakeColor.exe");
        //生存临时文件
        RandomAccessFile raf = new RandomAccessFile(file, "rwd");
        //设置临时文件的大小
        raf.setLength(length);
        raf.close();
        //计算出每个线程应该下载多少字节
        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 DownLoadThread(startIndex,endIndex,i).start();
          }
        }
      }catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   }
}



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

        public void run() {
            // TODO Auto-generated method stub
            //再次发送http请求,下载原文件
            HttpURLConnection conn;
            try {
                URL url = new URL(Multidload.path);
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5000);
                conn.setReadTimeout(5000);
                //设置本次http请求所请求的数据的区间
                conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);

                //请求部分数据,响应码是206
                 if(conn.getResponseCode()==206){
                     //流里此时只有1/3原文件的数据
                    InputStream is = conn.getInputStream();
                    byte[] b= new byte[1024];
                    int len =0;
ZZZ                    int total=0;
                    //拿到临时文件的输出流
                     File file = new File("TakeColor.exe");
                     //生存临时文件
                     RandomAccessFile raf = new RandomAccessFile(file, "rwd");
                     //把文件的写入位置移动至startIndex
                     raf.seek(startIndex);
                     while((len = is.read(b))!=-1){
                         raf.write(b, 0, len);
ZZZ                         total +=len;
ZZZ                      System.out.println("线程" +threadId +"下载了" +total);
                     }
                    raf.close();
                 }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
 }
0 0
原创粉丝点击