zoukankan      html  css  js  c++  java
  • Android 多线程下载网络文件

    这里需要用到一个类RandomAccessFile,下面是JDK里面对此类的描述!

    此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。存在指向该隐含数组的光标或索引,称为文件指针;输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。如果随机访问文件以读取/写入模式创建,则输出操作也可用;输出操作从文件指针开始写入字节,并随着对字节的写入而前移此文件指针。写入隐含数组的当前末尾之后的输出操作导致该数组扩展。该文件指针可以通过 getFilePointer 方法读取,并通过 seek 方法设置。

    通常,如果此类中的所有读取例程在读取所需数量的字节之前已到达文件末尾,则抛出 EOFException(是一种 IOException)。如果由于某些原因无法读取任何字节,而不是在读取所需数量的字节之前已到达文件末尾,则抛出 IOException,而不是 EOFException。需要特别指出的是,如果流已被关闭,则可能抛出 IOException。 

    该类有两个构造方法RandomAccessFile(File file/String name,String mode);

    其中Mode如下表

    r 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException
    rw 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件
    rws 打开以便读取和写入,对于 "rw",还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备
    rwd 打开以便读取和写入,对于 "rw",还要求对文件内容的每个更新都同步写入到底层存储设备
    
    
    


    下面是一个实例:

    public class MulThreadDownLoad {
        public static void main(String[] args) {
            String path = "http://localhost:4484/WebSite1/21.bmp";// 一个本地web服务,指向一张图片
            try {
                new MulThreadDownLoad().download(path, 3);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private void download(String path, int threadsize) throws Exception {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                int length = conn.getContentLength();
                File file = new File(getFilename(path));
                RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");
                // 在本地生成一个长度为length的文件
                accessFile.setLength(length);
                accessFile.close();
                // 每条线程负责下载的数据量
                int block = length / threadsize == 0 ? length / threadsize : length / threadsize + 1;
                for (int threadid = 0; threadid < threadsize; threadid++) {
                    new Thread(new Runa(threadid, block, url, file)).start();
                }
            } else
                System.out.println(conn.getResponseCode() + "");
        }
    
        private class Runa implements Runnable {
            private int threadid;
            private int block;
            private URL url;
            private File file;
    
            public Runa(int threadid, int block, URL url, File file) {
                this.threadid = threadid;
                this.block = block;
                this.url = url;
                this.file = file;
            }
    
            @Override
            public void run() {
                int start = threadid * block;// 计算该线程从网络文件的什么位置开始下载
                int end = (threadid + 1) * block - 1;// 计算该线程下载网络文件的结束位置
                try {
                    RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");// 创建从中读取和向其中写入(可选)的随机访问文件流
                    accessFile.seek(start);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(5000);
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("Range", "bytes=" + start + "-" + end);// 设置从start开始下载,到end结束
                    // if (conn.getResponseCode() == 200) {// 如果是分段下载,请求状态码不是200而是206!
                    InputStream ins = conn.getInputStream();
                    byte[] buffer = new byte[1024];
                    int length = 0;
                    while ((length = ins.read(buffer)) != -1) {
                        accessFile.write(buffer, 0, length);
                    }
                    accessFile.close();
                    ins.close();
                    // }
    
                } catch (Exception e) {
    
                    e.printStackTrace();
                }
            }
        }
        private String getFilename(String path) {
            return path.substring(path.lastIndexOf("/") + 1);
        }
    }

     下面是多线程下载原理图

    Android和JavaEE中,多线程下载原理相同。

  • 相关阅读:
    docker 容器管理常用命令
    第一章 入门示例
    rsyslog 日志服务器端配置
    如何利用一个按钮绑定两个事件
    select下拉框有了空行怎么办
    如何设置select下拉禁止选择
    mysql utf8 中文
    数据化决策的魅力
    数据化决策的魅力
    minor.major version 详解
  • 原文地址:https://www.cnblogs.com/a284628487/p/3033175.html
Copyright © 2011-2022 走看看