Android中 多线程下载原理

这里写图片描述

计算每个线程的下载起始终止位置公式如下
这里写图片描述

文件读写方式4中类型
这里写图片描述

工程源码目录
这里写图片描述

package cn.itcast.download;

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class MulThreadDownloader {

    public static void main(String[] args) throws Exception {
        String path = "http://219.245.72.241:8080/web3/gaosu.jsp";//服务器文件的地址
        int threadsize = 3;//开启三个线程,android应用中开启的线程数不能太多
        new MulThreadDownloader().download(path, threadsize);

    }

    private void download(String path, int threadsize) throws Exception {
        URL downpath = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) downpath.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");//生成本地文件
            accessFile.setLength(length);
            accessFile.close();
            //计算每条线程负责下载的数据量
            int block = length % threadsize == 0 ? length / threadsize : length / threadsize +1;
            for(int threadid = 0 ; threadid < threadsize ; threadid++){
                new DownloadThread(threadid, downpath, block, file).start();
            }
        }
    }
    //负责下载操作
    private final class DownloadThread extends Thread{
        private int threadid;
        private URL downpath;
        private int block;
        private File file;

        public DownloadThread(int threadid, URL downpath, int block, File file) {
            this.threadid = threadid;
            this.downpath = downpath;
            this.block = block;
            this.file = file;
        }
        public void run() {
            int startposition = threadid * block;//从网络文件的什么位置开始下载数据
            int endposition = (threadid+1) * block - 1;//下载到网络文件的什么位置结束
            //指示该线程要从网络文件的startposition位置开始下载,下载到endposition位置结束
            //Range:bytes=startposition-endposition
            try{
                RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");
                accessFile.seek(startposition);//移动指针到文件的某个位置
                HttpURLConnection conn = (HttpURLConnection) downpath.openConnection();
                conn.setConnectTimeout(5000);
                conn.setRequestMethod("GET");
                conn.setRequestProperty("Range", "bytes="+ startposition+ "-"+ endposition);//区域下载
                //分段下载请求码不是200,而是206
                //if(conn.getResponseCode() == 206){
                InputStream inStream = conn.getInputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while( (len = inStream.read(buffer)) != -1 ){
                    accessFile.write(buffer, 0, len);
                }
                accessFile.close();
                inStream.close();
                System.out.println("第"+ (threadid+1)+ "线程下载完成");
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 获取文件名称
     * @param path 下载路径
     * @return
     */
    private static String getFileName(String path) {
        return path.substring(path.lastIndexOf("/")+ 1);
    }

}

客户端要下载服务器web3目录里的gaosu.jsp文件
这里写图片描述

启动tomcat,挂起服务器web3
这里写图片描述

运行上面的java程序,结果展示
这里写图片描述

此时会发现工程源码目录下生成了下载完成的gaosu.jsp文件
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值