• java版本多线下载


    package com.jukay.httptask;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.RandomAccessFile;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class httptask {
        /**
         *  max_thread 线程个数
         *  url_path 一个网络地址
         *  file_obj 本地创建的文件,用来保存下载的地址
         */
        private int max_thread = 1;
        private String url_path = null;
        private File File_obj = null;
        
        /**
         * @param url 网络地址
         * @param num 线程数量
         * @param file 保存的文件
         */
        public httptask(String url , int num , File file){
            max_thread = num;
            url_path = url;
            File_obj = file;
        }
        
        /*
         * 开始网络任务
         */
        public void start(){
            int len_of_data;
            URL url = null;
            RandomAccessFile file = null;
            try {
                url = new URL(url_path);
                //文件大小
                len_of_data = url.openConnection().getContentLength();
                int block_num = (int)(len_of_data / max_thread);
                for(int thread_id = 1 ; thread_id <= max_thread ; ++thread_id){
                    int start = (thread_id-1)*block_num;
                    int end;
                    if(thread_id == max_thread){
                        end = len_of_data;
                    }else{
                        end = thread_id * block_num;
                    }
                    //开启现在线程
                    file = new RandomAccessFile(File_obj, "rw");
                    new Thread(new downUtils(url_path, start , end, file)).start();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        
        /*
         * 下载网络数据的工具类
         */
        class downUtils extends Thread{
            /**
             *
             * @param url_path 网络地址
             * @param startPoint 文件开始处
             * @param endPoint 文件结束处
             * @param file 需要保存的文件
             */
            public downUtils(String url_path, int startPoint, int endPoint, RandomAccessFile file) {
                this.url_path = url_path;
                this.startPoint = startPoint;
                this.endPoint = endPoint;
                this.file = file;
            }
            private String url_path;
            private int startPoint;
            private int endPoint;
            RandomAccessFile file = null;
            
            @Override
            public void run() {
                URL url = null;
                HttpURLConnection connection = null;
                InputStream input = null;
                byte[] buffer = new byte[1024*1024*4];
                int len = -1;
                    /**
                     * 文件移动到指定位置
                     */
                    try {
                        file.seek(startPoint);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
            
                
                /**
                 * 下载网络中的数据
                 */
                try {
                    url = new URL(url_path);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestProperty("Range", "bytes=" + startPoint + "-"    
                            + endPoint);
                    input = connection.getInputStream();
                    /**
                     * 将服务器返回的数据保存到文件中
                     */
                    while((len = input.read(buffer)) != -1){
                        file.write(buffer, 0, len);
                        System.out.println("---下载中---");
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    /**
                     * 关闭资源
                     */
                    if(input != null){
                        try {
                            input.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    if(file != null){
                        try {
                            file.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    }
                    System.out.println("下载完毕");
                }
            }
        }
    }

    下载速度杠杠的,比单线程快多了。

  • 相关阅读:
    数据库索引类型及实现方式
    MyBatis从入门到精通(十一):MyBatis高级结果映射之一对多映射
    解决克隆 centos虚拟机后修改克隆后的机器的ip、mac、uuid失败的问题
    多层表达式
    条件过滤
    复杂表达式
    生成列表
    迭代dict的key和value
    迭代dict的value
    索引迭代
  • 原文地址:https://www.cnblogs.com/shuimojun/p/4884028.html
Copyright © 2020-2023  润新知