欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java多线程下载文件原理解析

时间:2020-10-08 13:27:24|栏目:JAVA代码|点击:

原理解析:利用RandomAccessFile在本地创建一个随机访问文件,文件大小和服务器要下载的文件大小相同。根据线程的数量(假设有三个线程),服务器的文件三等分,并把我们在本地创建的文件同样三等分,每个线程下载自己负责的部分,到相应的位置即可。

示例图:

示例demo

import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class MutilDownload {
  private static String path = "http://192.168.80.85:8080/test.doc";
  private static final int threadCount = 3;

  public static void main(String[] args) {
    try {
      URL url = new URL(path);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      conn.setConnectTimeout(5000);
      int responseCode = conn.getResponseCode();
      if (responseCode == 200) {
        int contentLength = conn.getContentLength();
        System.out.println("length" + contentLength);

        RandomAccessFile rafAccessFile = new RandomAccessFile("test.doc", "rw");
        rafAccessFile.setLength(contentLength);

        int blockSize = contentLength / threadCount;
        for (int i = 0; i < threadCount; i++) {
          int startIndex = i * blockSize; //每个现成下载的开始位置
          int endIndex = (i + 1) * blockSize - 1;// 每个线程的结束位置
          if (i == threadCount - 1) {
            //最后一个线程
            endIndex = contentLength - 1;
          }

          new DownloadThread(startIndex, endIndex, i).start();
        }

      }
    } catch (Exception e) {

    }
  }

  private static class DownloadThread extends Thread {
    private int startIndex;
    private int endIndex;
    private int threadId;

    public DownloadThread(int startIndex, int endIndex, int threadId) {
      this.startIndex = startIndex;
      this.endIndex = endIndex;
      this.threadId = threadId;
    }

    @Override
    public void run() {
      try {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); //固定写法,请求部分资源
        int responseCode = conn.getResponseCode(); // 206表示请求部分资源
        if (responseCode == 206) {
          RandomAccessFile rafAccessFile = new RandomAccessFile("test.doc", "rw");
          rafAccessFile.seek(startIndex);
          InputStream is = conn.getInputStream();
          int len = -1;
          byte[] buffer = new byte[1024];
          while ((len = is.read(buffer)) != -1) {
            rafAccessFile.write(buffer, 0, len);
          }
          rafAccessFile.close();

          System.out.println("线程" + threadId + "下载完成");
        }
      } catch (Exception e) {

      }
    }
  }
}

上一篇:Java编程中线程池的基本概念和使用

栏    目:JAVA代码

下一篇:Java jdbc批量多线程读取CVS文件入库

本文标题:java多线程下载文件原理解析

本文地址:http://www.codeinn.net/misctech/8484.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有