欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现多线程文件下载的代码示例

程序员文章站 2024-03-05 17:32:19
java实现多线程文件下载思路: 1、基本思路是将文件分段切割、分段传输、分段保存。 2、分段切割用到httpurlconnection对象的setrequestpro...

java实现多线程文件下载思路:

1、基本思路是将文件分段切割、分段传输、分段保存。

2、分段切割用到httpurlconnection对象的setrequestproperty("range", "bytes=" + start + "-" + end)方法。

3、分段传输用到httpurlconnection对象的getinputstream()方法。

4、分段保存用到randomaccessfile的seek(int start)方法。

5、创建指定长度的线程池,循环创建线程,执行下载操作。 

 首先,我们要先写一个方法,方法的参数包含url地址,保存的文件地址,切割后的文件开始位置和结束位置,这样我们就能把分段文件下载到本地。并且这个方法要是run方法,这样我们启动线程时就直接执行该方法。

public class downloadwithrange implements runnable
  {
    private string urllocation;

    private string filepath;

    private long start;

    private long end;

    downloadwithrange(string urllocation, string filepath, long start, long end)
    {
      this.urllocation = urllocation;
      this.filepath = filepath;
      this.start = start;
      this.end = end;
    }

    @override
    public void run()
    {
      try
      {
        httpurlconnection conn = gethttp();
        conn.setrequestproperty("range", "bytes=" + start + "-" + end);

        file file = new file(filepath);
        randomaccessfile out = null;
        if (file != null)
        {
          out = new randomaccessfile(file, "rwd");
        }
        out.seek(start);
        inputstream in = conn.getinputstream();
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = in.read(b)) != -1)
        {
          out.write(b, 0, len);
        }
        in.close();
        out.close();
      }
      catch (exception e)
      {
        e.getmessage();
      }

    }

    public httpurlconnection gethttp() throws ioexception
    {
      url url = null;
      if (urllocation != null)
      {
        url = new url(urllocation);
      }
      httpurlconnection conn = (httpurlconnection) url.openconnection();
      conn.setreadtimeout(5000);
      conn.setrequestmethod("get");

      return conn;
    }

  }

然后我们创建线程池,线程池的长度可以自定义,然后循环创建线程来执行请求,每条线程的请求开始位置和结束位置都不同,本地存储的文件开始位置和请求开始位置相同,这样就可以实现多线程下载了。

public class downloadfilewiththreadpool
{
  public void getfilewiththreadpool(string urllocation,string filepath, int poollength) throws ioexception
  {
    executor threadpool = executors.newfixedthreadpool(poollength);
    
    long len = getcontentlength(urllocation);
    for(int i=0;i<poollength;i++)
    {
      long start=i*len/poollength;
      long end = (i+1)*len/poollength-1;
      if(i==poollength-1)
      {
        end =len;
      }
      downloadwithrange download=new downloadwithrange(urllocation, filepath, start, end);
      threadpool.execute(download);
    }
  }

  public static long getcontentlength(string urllocation) throws ioexception
  {
    url url = null;
    if (urllocation != null)
    {
      url = new url(urllocation);
    }
    httpurlconnection conn = (httpurlconnection) url.openconnection();
    conn.setreadtimeout(5000);
    conn.setrequestmethod("get");
    long len = conn.getcontentlength();

    return len;
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。