栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

多线程下载

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多线程下载

实现类DownLoad

public class DownLoad extends Thread{
   
   private int startIndex;//开始位置
   private int endIndex;//结束位置
   private int threadId;//线程编号
   public DownLoad(int startIndex, int endIndex, int threadId) {
      super();
      this.startIndex = startIndex;
      this.endIndex = endIndex;
      this.threadId = threadId;
   }
   
   
   @Override
   public void run() {
      long start = System.currentTimeMillis();
      try {
//        2.通过统一资源管理器URL,和要下载的资源建立连接
         URL url = new URL(DownLoadTest.path);
         //获取联系
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//       设置参数信息
         conn.setRequestMethod("GET");//get方式请求
         conn.setConnectTimeout(5000);//连接超时
         conn.setReadTimeout(5000);//读取超时
         conn.addRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);//设置读取的范围,固定格式的
         System.out.println(this.currentThread().getName()+"  "+threadId+":下载的文件范围:"+startIndex+"-"+endIndex);
         if(conn.getResponseCode()==206) {//部分数据ok
//           3.获取网络资源的输入流,得到数据
            InputStream is = conn.getInputStream(); 
            
//           4.创建文件存放的file对象,
            File distFile = new File("D:\upload\mysql-8.0.25-winx64.zip");
            //创建读写随机流
            RandomAccessFile raf = new RandomAccessFile(distFile, "rwd");
            raf.seek(startIndex);//指定写入的开始位置

//           5.具体文件下载的实现
            byte[] buf =new byte[8192];
            int len = 0;
            while((len= is.read(buf))!=-1) {
               raf.write(buf, 0, len);
            }
             
//           6.关闭流
            raf.close();
            is.close();
            //线程完成次数加1
            DownLoadTest.okThread++;
            //加锁
            synchronized (DownLoadTest.path) {
               System.out.println(this.currentThread().getName()+"  "+threadId+":线程下载完成!"+DownLoadTest.okThread);
               //判断如果线程完成数量达到了线程数,重置成0
               if(DownLoadTest.okThread==DownLoadTest.threadCount) {
                  DownLoadTest.okThread=0;
               }
            }
         }
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

      long end = System.currentTimeMillis();
      System.out.println(this.currentThread().getName()+"下载子线程运行的时间:"+(end-start));
      
   }

}

测试类
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class DownLoadTest {
   
   static String path = "https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.25-winx64.zip"; //链接
   static int threadCount=1;//线程的数量
   static int okThread=0;//已经下载完成的线程数量
   
   
   public static void main(String[] args) throws Exception {
      long start = System.currentTimeMillis();
//    通过统一资源管理器URL,和要下载的资源建立连接
      URL url = new URL(DownLoadTest.path);
      //获取联系
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//        设置参数信息
      conn.setRequestMethod("GET");//get方式请求
      conn.setConnectTimeout(5000);//连接超时
      conn.setReadTimeout(5000);//读取超时
      
      if(conn.getResponseCode()==200) {
         int count = conn.getContentLength();//文件的长度
         int size = count / threadCount;//每个线程需要下载的文件长度
         System.out.println(count);
         System.out.println(size);
         for (int i = 0; i < threadCount; i++) {
            int startIndex = size*i;
            int endIndex = size*(i+1);
            DownLoad thread= new DownLoad(startIndex, endIndex, i);
            thread.start();
         }
         
      }
      long end = System.currentTimeMillis();
      System.out.println("主线的时间:"+(end-start));
   }
}
运行效果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/323036.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号