public class MultiThreadDown {    static int ThreadCount = 4;        static String path = "http://192.168.1.104:8080/android/pdf.exe";    static long start = 0;    public static void main(String[] args) throws Exception {        URL url = new URL(path);        HttpURLConnection conn = (HttpURLConnection) url.openConnection();        conn.setReadTimeout(5000);        conn.setRequestMethod("GET");        conn.setConnectTimeout(5000);        if (conn.getResponseCode() == 200) {            // 1.先获取请求资源的大小            int length = conn.getContentLength();            File file = new File("m.mpg");            // 生成临时文件            RandomAccessFile raf = new RandomAccessFile(file, "rwd");            // 设置临时文件的大小            raf.setLength(length);            raf.close();            // 计算每个线程应该要下载多少个字节            int size = length / ThreadCount;            start = System.currentTimeMillis();            for (int i = 0; i < ThreadCount; i++) {                // 计算线程下载的开始位置和结束位置                int startIndex = i * size;                int endIndex = (i + 1) * size - 1;                // 如果是最后一个线程,那么结束位置写死                if (i == (ThreadCount - 1)) {                    endIndex = length - 1;                }                new DownThread(startIndex, endIndex, i).start();            }        }    }}class DownThread extends Thread {    int startIndex;    int endIndex;    int threadId;    public DownThread(int startIndex, int endIndex, int threadId) {        super();        this.startIndex = startIndex;        this.endIndex = endIndex;        this.threadId = threadId;    }    @Override    public void run() {        // 再次发送HTTP请求,下载源文件        try {            URL url = new URL(MultiThreadDown.path);            HttpURLConnection conn = (HttpURLConnection) url.openConnection();            conn.setReadTimeout(5000);            conn.setRequestMethod("GET");            conn.setConnectTimeout(5000);            // 设置请求的数据的区间            conn.setRequestProperty("Range", "bytes=" + startIndex + "-"                    + endIndex);            // 请求部分数据,响应码为206            if (conn.getResponseCode() == 206) {                // 此时只有 1/threadcount数据                InputStream is = conn.getInputStream();                byte[] b = new byte[1024];                int len = 0;                long total = 0;                File file = new File("m.mpg");                RandomAccessFile raf = new RandomAccessFile(file, "rwd");                // 把文件的写入位置移动至startIndex                raf.seek(startIndex);                while ((len = is.read(b)) != -1) {                    // 每次读取流里的数据写入临时文件                    raf.write(b, 0, len);                    total += len;                }            }        } catch (IOException e) {            e.printStackTrace();        }    }}  

更多相关文章

  1. Android-Handler的消息传递机制
  2. Android中的线程模型
  3. Android(安卓)开发技术周报 Issue#299
  4. Android(安卓)Java 中Thread与Runnable的区别
  5. java版android Handler机制模型
  6. Android(安卓)消息机制 你了解多少
  7. Android高级进阶之路【六】Android(安卓)Framework解析
  8. Android(安卓)Retrofit通过OkHttp添加Interceptor拦截器设置Get
  9. Android之应用首次使用的欢迎界面实例

随机推荐

  1. Android与Unity交互调用mUnityPlayer.qui
  2. Android第二天 视图及五大常用布局
  3. 【小萌伴Android】相关文章目录
  4. New Graphics Improvement in Android 4.
  5. android 带你从源码的角度解析Scroller的
  6. android 笔记 --- Android各种访问权限Pe
  7. Android读书笔记之Android布局小结
  8. Android Runtime Permission 详解
  9. Android Apk 反编译,Apk 修改以后再编译打
  10. FFmpeg 中的 log 输出到 Android(安卓)的