欢迎来到代码驿站!

JAVA代码

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

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

时间:2021-04-01 08:19:06|栏目:JAVA代码|点击:

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;
  }

上一篇:java多线程有序读取同一个文件

栏    目:JAVA代码

下一篇:Java线程等待用法实例分析

本文标题:Java实现多线程文件下载的代码示例

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

推荐教程

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

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

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

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

Copyright © 2020 代码驿站 版权所有