当前位置: 移动技术网 > IT编程>开发语言>Java > java多线程下载实例详解

java多线程下载实例详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java多线程下载。分享给大家供大家参考,具体如下:

使用多线程下载文件可以更快完成文件的下载,多线程下载文件之所以快,是因为其抢占的服务器资源多。如:假设服务器同时最多服务100个用户,在服务器中一条线程对应一个用户,100条线程在计算机中并非并发执行,而是由cpu划分时间片轮流执行,如果a应用使用了99条线程下载文件,那么相当于占用了99个用户的资源,假设一秒内cpu分配给每条线程的平均执行时间是10ms,a应用在服务器中一秒内就得到了990ms的执行时间,而其他应用在一秒内只有10ms的执行时间。就如同一个水龙头,每秒出水量相等的情况下,放990毫秒的水肯定比放10毫秒的水要多。

多线程下载的实现过程:

1.首先得到下载文件的长度,然后设置本地文件的长度。

httpurlconnection.getcontentlength();
randomaccessfile file = new randomaccessfile("youdao.exe","rw");
file.setlength(filesize);//设置本地文件的长度

2.根据文件长度和线程数计算每条线程下载的数据长度和下载位置。如:文件的长度为6m,线程数为3,那么,每条线程下载的数据长度为2m,每条线程开始下载的位置如下图所示。

3.使用http的range头字段指定每条线程从文件的什么位置开始下载,如:指定从文件的2m位置开始下载文件,代码如下:

复制代码 代码如下:
httpurlconnection.setrequestproperty("range", "bytes=2097152-");

4.保存文件,使用randomaccessfile类指定每条线程从本地文件的什么位置开始写入数据

randomaccessfile threadfile = new randomaccessfile("<span style="font-family: arial, helvetica, sans-serif;">youdao.exe</span><span style="font-family: arial, helvetica, sans-serif;"> ","rw");</span>
threadfile.seek(2097152);//从文件的什么位置开始写入数据

下面是通过具体实现类:

在写实现类之前我们首先要将要下载的文件放在服务器上并部署:

我是放在了这里 d:\tomcat\apache-tomcat-7.0.37\webapps\doudou目录下,并启动d:\tomcat\apache-tomcat-7.0.37\bin下的startup.bat

1.downloadtest.java

package www.csdn.net.down;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
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 downloadtest {
 public file file;
 public randomaccessfile accessfile;
 // 线程的数量
 public static int threadnum = 3;
 // 每个线程负责下载的大小
 int blocksize;
 // 创建访问的路径
 public string path = "http://localhost:8080/doudou/youdao.exe";
 public static int threadcount;// 数量
 public void testdown() {
  try {
   // 创建出url对象
   url url = new url(path);
   // 创建出 httpurlconnection对象
   httpurlconnection httpurlconnection = (httpurlconnection) url
     .openconnection();
   // 设置 发请求发送的方式
   httpurlconnection.setrequestmethod("get");
   // 设置请求是否超时时间
   httpurlconnection.setconnecttimeout(5000);
   // 设置
   httpurlconnection
     .setrequestproperty("user-agent",
       " mozilla/5.0 (compatible; msie 10.0; windows nt 6.2; trident/6.0)");
   // 是否响应成功
   if (httpurlconnection.getresponsecode() == 200) {
    // 获取文件的大小
    int size = httpurlconnection.getcontentlength();
    system.out.println("文件的大小" + size);
    // 创建文件
    file = new file("youdao.exe");
    accessfile = new randomaccessfile(file, "rwd");
    // 设置文件的大小
    accessfile.setlength(size);
    // 每个线程下载的大小
    blocksize = size / threadnum;
    // 开三个线程 操作此文件
    for (int i = 1; i <= threadnum; i++) {
     // 1 2 3
     // 计算出每个线程开始的位置
     int startsize = (i - 1) * blocksize;
     // 结束位置
     int endsize = (i) * blocksize;
     // 当线程是最后一个线程的时候
     if (i == threadnum) {
      // 判断文件的大小是否大于计算出来的结束位置
      if (size > endsize) {
       // 结束位置 等于 文件的大小
       endsize = size;
      }
     }
     // 为每个线程创建一个随机的读取
     randomaccessfile threadaccessfile = new randomaccessfile(
       file, "rwd");
     new thread(new downloadthread(i, threadaccessfile,
       startsize, endsize, path)).start();
    }
   }
  } catch (malformedurlexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (ioexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
 }
 public static void main(string[] args) {
  downloadtest downloadtest = new downloadtest();
  // 调用下载方法
  downloadtest.testdown();
 }
}
class downloadthread implements runnable {
 // 下载文件的封装
 public randomaccessfile accessfile; // 每个线程 都拥有一个accessfile的文件对象 线程1 线程2 线程3
 // 线程下载文件的起始位置
 public int startsize;
 public int endsize;
 // 文件下载的path路径
 public string path;
 public int threadid; // 线程的标识
 public downloadthread(int threadid, randomaccessfile accessfile,
   int startsize, int endsize, string path) {
  this.threadid = threadid;
  this.accessfile = accessfile;
  this.startsize = startsize;
  this.endsize = endsize;
  this.path = path;
 }
 @override
 public void run() {
  // 执行run方法
  try {
   // 创建文件
   file threadfile = new file(threadid + ".txt");
   if (threadfile.exists()) {
    // 读取该文件的内容
    // 创建文件的输入流对象
    fileinputstream fis = new fileinputstream(threadfile);
    // 采用工具类读取
    byte data[] = streamtools.istodata(fis);
    // 转化成字符串
    string threadlen = new string(data);
    if ((threadlen != null) && (!"".equals(threadlen))) {
     startsize = integer.valueof(threadlen);
     // 解决 416bug的错误
     if (startsize > endsize) {
      startsize = endsize - 1;
     }
    }
   }
   // 创建url对象
   url url = new url(path);
   // 创建httpurlconnection对象
   httpurlconnection httpurlconnection = (httpurlconnection) url
     .openconnection();
   // 设置请求的头
   httpurlconnection.setrequestmethod("get");
   // 设置请求是否超时时间
   httpurlconnection.setconnecttimeout(5000);
   // 设置
   httpurlconnection
     .setrequestproperty("user-agent",
       " mozilla/5.0 (compatible; msie 10.0; windows nt 6.2; trident/6.0)");
   // 关键的设置
   httpurlconnection.setrequestproperty("range", "bytes=" + startsize
     + "-" + endsize);
   // 输出当前线程
   system.out.println("当前线程" + threadid + " 下载开始位置:" + startsize
     + " 下载结束位置:" + endsize);
   // 响应成功
   // 设置随机读取文件的 开始位置
   accessfile.seek(startsize);
   // 获取相应流对象
   inputstream is = httpurlconnection.getinputstream();
   // 创建输出流对象
   byte buffer[] = new byte[1024];
   int len = 0;
   int threadtotal = 0;// 每个线程下载后保存记录 /
   while ((len = is.read(buffer)) != -1) {
    accessfile.write(buffer, 0, len);
    threadtotal += len;// 记录你写入的长度 //xml文件
    // 通过文件记录文件下载的长度
    fileoutputstream fos = new fileoutputstream(threadfile);
    fos.write((threadtotal + "").getbytes());
    fos.flush();
    fos.close();
   }
   accessfile.close();
   is.close();
   system.out.println(threadid + "线程执行完毕");
   //线程操作
   synchronized (downloadtest.class) {
    downloadtest.threadcount++;
    if (downloadtest.threadcount >= downloadtest.threadnum) {
     for(int i=1;i<=downloadtest.threadnum;i++){
      file file = new file(i+".txt");
      if(file.exists()){
       file.delete();
      }
     }
    }
   }
  } catch (malformedurlexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (ioexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
 }
}

2.流工具的封装 streamtools.java

package www.csdn.net.down;
import java.io.bytearrayoutputstream;
import java.io.ioexception;
import java.io.inputstream;
public class streamtools {
 public static byte[] istodata(inputstream is) throws ioexception{
  // 字节输出流
  bytearrayoutputstream bops = new bytearrayoutputstream();
  // 读取数据的缓存区
  byte buffer[] = new byte[1024];
  // 读取长度的记录
  int len = 0;
  // 循环读取
  while ((len = is.read(buffer)) != -1) {
   bops.write(buffer, 0, len);
  }
  // 把读取的内容转换成byte数组
  byte data[] = bops.tobytearray();
  bops.flush();
  bops.close();
  is.close();
  return data;
 }
}

希望本文所述对大家java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网