java实现多线程文件的断点续传

 更新时间:2022年06月24日 09:22:38   作者:qq_38844040  
这篇文章主要为大家详细介绍了java实现多线程文件的断点续传,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

java文件的多线程断点续传大致原理,供大家参考,具体内容如下

谈到文件断点续传那么就离不开java.io.RandomAcessFile HttpUrlConnection类

大致思路如下:

1、HttpUrlConnection去请求服务器 获得文件的长度con.getContentLength()
2、创建一个空的RandomAcessFile来接收,并且指定刚刚获取的长度setLength
3、开启N个线程 计算每个线程需要下载的长度
4、获取之前先去看看下载的进度保存文件是否存在 如果存在就从文件里获取已经下载的进度
5、开始文件下载
6、临时文件的删除 资源的关闭

下面贴出完整代码

package demo;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

class MultiDownloaFile {
    public static final String path = "http://192.168.217.1:8080/androidsimpleserver/HTTP.pdf";
    public static final int TOTAL_THREAD_COUNT = 3;
    public static int runningThreadCount = 0;

    public static void main(String[] args) {
        try {
            long start = System.currentTimeMillis();
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            int code = conn.getResponseCode();
            if (code == 200) {
                int length = conn.getContentLength();
                System.out.println("file length:" + length);
                //create  a  null  file  to  save  its  length
                RandomAccessFile raf = new RandomAccessFile(getDownloadFileName(path), "rw");
                raf.setLength(length);
                raf.close();
                //parse  every  thread  that  they  need  how  much  room  to  download
                int blockSize = length / TOTAL_THREAD_COUNT;
                System.out.println("every block size:" + blockSize);
                runningThreadCount = TOTAL_THREAD_COUNT;
                for (int threadId = 0; threadId < TOTAL_THREAD_COUNT; threadId++) {
                    int startPosition = threadId * blockSize;
                    int endPosition = (threadId + 1) * blockSize - 1;
                    if (threadId == (TOTAL_THREAD_COUNT - 1)) {
                        endPosition = length - 1;
                    }
                    System.out.println("thread::" + threadId + " download range:" + startPosition + "~~" + endPosition);
                    //start thread  to  download
                    new DownloadThread(threadId, startPosition, endPosition).start();
                }
            } else {
                System.out.println(" connection  error ");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从网络路径获取文件名
     *
     * @param path 网络路径
     * @return 文件名
     */
    private static String getDownloadFileName(String path) {
        return path.substring(path.lastIndexOf("/") + 1);
    }

    /**
     * 下载文件的线程
     */
    private static class DownloadThread extends Thread {
        /**
         * 线程id
         */
        private int threadId;

        /**
         * 当前线程下载的起始位置
         */
        private int startPosition;

        /**
         * 当前线程下载的终止位置
         */
        private int endPosition;

        public DownloadThread(int threadId, int startPosition, int endPosition) {
            this.threadId = threadId;
            this.startPosition = startPosition;
            this.endPosition = endPosition;
        }

        @Override
        public void run() {
            System.out.println("thread:" + threadId + " begin working");
            // lest thread download it's self range data
            try {

                File finfo = new File(TOTAL_THREAD_COUNT + getDownloadFileName(path) + threadId + ".txt");
//                断点续传
                if (finfo.exists() && finfo.length() > 0) {
                    System.out.println(" 断点续传开始");
                    FileInputStream fis = new FileInputStream(finfo);
                    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                    String lastPosition = br.readLine();
                    // This thread download data before times;
                    int intLastPosition = Integer.parseInt(lastPosition);
                    startPosition = intLastPosition;
                    fis.close();
                }

                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                System.out.println("begin and end:" + threadId + " range of download: " + startPosition + "~~" + endPosition);
                conn.setRequestProperty("Range", "bytes=" + startPosition + "-" + endPosition);
                // Download Resource from server
                int code = conn.getResponseCode();
                if (code == 206) {
                    InputStream is = conn.getInputStream();
//                    RandomAccessFile raf = new RandomAccessFile(getDownloadFileName(path), "rw");
                    RandomAccessFile raf = new RandomAccessFile(getDownloadFileName(path), "rw");

                    // vary important, position of begin to write
                    raf.seek(startPosition);
                    byte[] buffer = new byte[1024 * 100];
                    int len = -1;
                    int total = 0; // downloaded data of current thread in this times;
                    while ((len = is.read(buffer)) != -1) {
                        raf.write(buffer, 0, len);
                        // record position of current thread to downloading
                        total += len;
                        RandomAccessFile inforaf = new RandomAccessFile(TOTAL_THREAD_COUNT + getDownloadFileName(path) + threadId + ".txt", "rwd");
                        // save position of current thread
                        inforaf.write(String.valueOf(startPosition + total).getBytes());
                        inforaf.close();
                    }
                    is.close();
                    raf.close();
                    System.out.println("thread:" + threadId + " download complete...");
                } else {
                    System.out.println("request download failed.");
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                synchronized (MultiDownloaFile.class) {
                    runningThreadCount--;
                    if (runningThreadCount <= 0) {
                        System.out.println("  all  multi thread download complete.   success!!!");
                        for (int i = 0; i < TOTAL_THREAD_COUNT; i++) {
                            File finfo = new File(TOTAL_THREAD_COUNT + getDownloadFileName(path) + i + ".txt");
                            System.out.println(finfo.delete());
                        }
                    }
                }
            }
        }
    }
}

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

相关文章

  • 基于Java实现QQ登录注册功能的示例代码

    基于Java实现QQ登录注册功能的示例代码

    这篇文章主要和大家分享如何利用Java语言实现QQ登录、注册等功能。本文主要应用的技术有:GUI、JDBC、多线程等,需要的可以参考一下
    2022-05-05
  • Java编程常见内存溢出异常与代码示例

    Java编程常见内存溢出异常与代码示例

    这篇文章主要介绍了Java编程常见内存溢出异常与代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • java实现商品信息管理系统

    java实现商品信息管理系统

    这篇文章主要为大家详细介绍了java实现商品信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • spring配置扫描多个包问题解析

    spring配置扫描多个包问题解析

    这篇文章主要介绍了spring配置扫描多个包问题解析,具有一定参考价值,需要的朋友可以了解下。
    2017-10-10
  • 两个例子了解java中的回调机制

    两个例子了解java中的回调机制

    这篇文章主要介绍了Java中回调机制的相关资料,帮助大家更好的理解和学习使用Java,感兴趣的朋友可以了解下
    2021-02-02
  • org.apache.ibatis.binding.BindingException异常报错原因以及详细解决方案

    org.apache.ibatis.binding.BindingException异常报错原因以及详细解决方案

    这篇文章主要给大家介绍了关于org.apache.ibatis.binding.BindingException异常报错原因以及详细解决方案的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-02-02
  • 详解Java中Optional类的使用方法

    详解Java中Optional类的使用方法

    Optional的作用是什么?他都有哪些方法?阿里规范点名说尽量用Optional来避免空指针,那么什么场景用Optional?本篇文章围绕这三点来进行讲解,感兴趣的可以学习一下
    2022-05-05
  • Kafka中Producer和Consumer的作用详解

    Kafka中Producer和Consumer的作用详解

    这篇文章主要介绍了Kafka中Producer和Consumer的作用详解,Kafka是一个分布式的流处理平台,它的核心是消息系统,Producer是Kafka中用来将消息发送到Broker的组件之一,它将消息发布到主题,并且负责按照指定的分区策略将消息分配到对应的分区中,需要的朋友可以参考下
    2023-12-12
  • Java实现redis分布式锁的三种方式

    Java实现redis分布式锁的三种方式

    本文主要介绍了Java实现redis分布式锁的三种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • 浅析Spring容器原始Bean是如何创建的

    浅析Spring容器原始Bean是如何创建的

    这篇文章主要是想和小伙伴们一起聊聊 Spring 容器创建 Bean 最最核心的 createBeanInstance 方法,文中的示例代码讲解详细,需要的可以参考一下
    2023-08-08

最新评论