多线程下载

多线程下载

步骤

多线程下载实现类

建立成员变量,描述多线程下载需要的参数

通过统一资源管理器URL,和要下载的资源建立连接

设置参数信息

获取网络资源的输入流,得到数据

创建文件存放的file对象

具体文件下载的实现

关闭流

多线程类

package com.zmc;
​
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
​
public class ThreadDonwload implements Runnable{
   private int begin;//开始位置
   private int end;//结束位置
   private int id;//线程编号
   public ThreadDonwload(int begin, int end, int id) {
      super();
      this.begin = begin;
      this.end = end;
      this.id = id;
   }
   @Override
   public void run() {
      try {
          //通过统一资源管理器URL,和要下载的资源建立连接
         URL url = new URL(ThreadDownLoadTest.path);
          //获取联系
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
          //设置参数信息
         urlConnection.setRequestMethod("GET");//get方式请求
         urlConnection.setConnectTimeout(10000);//连接超时
         urlConnection.setReadTimeout(10000);//读取超时
         urlConnection.addRequestProperty("Range", "bytes="+begin+"-"+end);//设置读取的范围,固定格式的
         System.out.println("线程"+Thread.currentThread().getName()+"--"+id+"下载的范围是"+begin+"---"+end);
         if(urlConnection.getResponseCode()==206) {//部分数据完成
             //获取网络资源的输入流,得到数据
            InputStream is = urlConnection.getInputStream();
             //创建文件存放的file对象,
            File file = new File("D:\\AAAA\\zmc.zip");
             //创建读写随机流
            RandomAccessFile accessFile = new RandomAccessFile(file, "rws");
            accessFile.seek(begin);//指定写入的开始位置
             //具体文件下载的实现
            byte[] b =new byte[8192];
            int len = 0;
            while((len= is.read(b))!=-1) {
               accessFile.write(b, 0, len);
            }
             //关闭流
            accessFile.close();
            is.close();
             //线程完成次数加1
            ThreadDownLoadTest.finishNum++;
             //加锁
            synchronized (ThreadDownLoadTest.path) {
               System.out.println("线程:"+Thread.currentThread().getName()+id+"下载完成"
                     + ThreadDownLoadTest.finishNum);
               if(ThreadDownLoadTest.finishNum== ThreadDownLoadTest.threadNum) {
                   //判断如果线程完成数量达到了线程数,重置成0
                  ThreadDownLoadTest.finishNum=0;
               }
            }
         }
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
​
}

主类

package com.zmc;
​
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
​
public class ThreadDownLoadTest {
   static String path = "http://y.downya.com/soft/netfx20sp1x64_downyi.com.zip";
    //线程的数量
   static int threadNum=10;
    //已经下载完成的线程数量
   static int finishNum=0;
    //通过统一资源管理器URL,和要下载的资源建立连接
   public static void main(String[] args){
      try {
      URL url = new URL(ThreadDownLoadTest.path);
      //获取联系
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      //设置参数信息
      urlConnection.setRequestMethod("GET");
      urlConnection.setConnectTimeout(20000);
      urlConnection.setReadTimeout(20000);
​
      if (urlConnection.getResponseCode()==200) {
      //文件的长度
         int length = urlConnection.getContentLength();
         //每个线程需要下载的文件长度
         int size = length/threadNum;
         for (int i = 0; i < threadNum; i++) {
            int begin=size*i;
            int end=size*(i+1);
            ThreadDonwload donwload = new ThreadDonwload(begin,end, i);
            Thread thread = new Thread(donwload);
            thread.start();
         }
      }
   }catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值