博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程断点续传下载
阅读量:6923 次
发布时间:2019-06-27

本文共 3643 字,大约阅读时间需要 12 分钟。

hot3.png

使用多线程下载文件可以更快的完成文件的下载,多线程下载之所以快,是因为其抢占的服务器资源多。

多线程下载实现的过程:

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

    HttpURLConnection.getContentLength();

    RandomAccessFile file=new RandowAccessFile("qq.exe","rwd");

    file.setLength(filesize);//设置本地文件的长度

2,根据文件长度和线程数计算每条线程下载的数据长度和下载位置

3,使用Http的Range字段指定每条线程从文件的什么位置下载,下载到什么位置

     HttpURLConnection.setRequestProperty("Range","byte=500-20000");

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

    RandomAccessFile file=new RandowAccessFile("qq.exe","rwd");

    file.seek(500);//设置从文件的什么位置开始写入数据

 

public class FileDownLoader {

 

  public void download() throws Exception {

  String path = "http://www.qq.com/qq.exe";

  URL url = new URL(path);

  HttpURLConnection conn = (HttpURLConnection) url.openConnection();

  conn.setConnectTimeout(5*1000);

  conn.setRequestMethod("GET");

  conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");

  conn.setRequestProperty("Accept-Language", "zh-CN");

  conn.setRequestProperty("Charset", "UTF-8");

  conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");

  conn.setRequestProperty("Connection", "Keep-Alive");

  System.out.println(conn.getResponseCode());

 

  int filesize = conn.getContentLength();//得到文件大小

  conn.disconnect();

  int threasize = 3;//线程数

  int perthreadsize = filesize / 3 + 1;

  RandomAccessFile file = new RandomAccessFile("102.wma","rw");

  file.setLength(filesize);//设置本地文件的大小

  file.close();

  for(int i=0; i<threasize ; i++){

  int startpos = i * perthreadsize;//计算每条线程的下载位置

  RandomAccessFile perthreadfile = new RandomAccessFile("102.wma","rw");//

  perthreadfile.seek(startpos);//从文件的什么位置开始写入数据

  new DownladerThread(i, path, startpos, perthreadsize, perthreadfile).start();

  }

  //以下代码要求用户输入q才会退出测试方法,如果没有下面代码,会因为进程结束而导致进程内的下载线程被销毁

  int quit = System.in.read();

  while('q'!=quit){

  Thread.sleep(2 * 1000);

  }

  }

 

  private class DownladerThread extends Thread{

  private int startpos;//从文件的什么位置开始下载

  private int perthreadsize;//每条线程需要下载的文件大小

  private String path;

  private RandomAccessFile file;

  private int threadid;

 

  public DownladerThread(int threadid, String path, int startpos, int perthreadsize, RandomAccessFile perthreadfile) {

  this.path = path;

  this.startpos = startpos;

  this.perthreadsize = perthreadsize;

  this.file = perthreadfile;

  this.threadid = threadid;

  }

 

  @Override

  public void run() {

  try {

  URL url = new URL(path);

  HttpURLConnection conn = (HttpURLConnection)url.openConnection();

  conn.setConnectTimeout(5 * 1000);

  conn.setRequestMethod("GET");

  conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");

  conn.setRequestProperty("Accept-Language", "zh-CN");

  conn.setRequestProperty("Charset", "UTF-8");

  conn.setRequestProperty("Range", "bytes=" + this.startpos + "-");

  InputStream inStream = conn.getInputStream();

  byte[] buffer = new byte[1024];

  int len = 0;

  int length = 0;

  while(length<perthreadsize && (len = inStream.read(buffer))!=-1){

  file.write(buffer, 0, len);

  length += len;//累计该线程下载的总大小

  }

  file.close();

  inStream.close();

  System.out.println(threadid+ "线程完成下载");

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

}

转载于:https://my.oschina.net/u/725837/blog/113434

你可能感兴趣的文章
关于eclipse的ADT(插件)对xml的android:text属性检查修改
查看>>
Davinci视频采集驱动文档
查看>>
.NET运用AJAX 总结及其实例
查看>>
IBM,SUN,JAVA,ECLIPSE原来也有小故事
查看>>
PLSQL_海量数据处理系列1_架构
查看>>
苹果坐标2013百度之星4.27月赛 题目二 Apple
查看>>
继承ViewGroup:重写onMeasure方法和onLayout方法
查看>>
服务器安装centos6.4 x86_64 与 配置XManager的XDMCP服务 与 配置Ntfs读写
查看>>
序列遍历hdu 4545(水题,不是DP)
查看>>
网络字节序与主机字节序
查看>>
Flex 事件机制
查看>>
使用weinre对WebApp页面dom进行远程调试
查看>>
android 应用级别 亮度调节
查看>>
FGMap 更新说明
查看>>
如何:在 SharePoint 中创建外部列表
查看>>
C++ 中常见预定义宏的使用
查看>>
xml操作
查看>>
Java中常见的异常__
查看>>
如何将图片保存至自定义分组
查看>>
使用jQuery简单实现产品展示的图片左右滚动功能
查看>>