Java中如何实现下载多线程文件

发布时间:2020-11-10 17:04:14 作者:Leah
来源:亿速云 阅读:145

今天就跟大家聊聊有关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中如何实现下载多线程文件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. java多线程下载
  2. 怎么在Java中利用多线程实现分片下载文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java 多线程 ava

上一篇:Mysql数据库中的事务并发如何解决

下一篇:深入浅析Java中的回调机制

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》