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("下载完毕");
            }
        }
    }
}

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

posted @ 2015-10-16 00:02  水墨沙场  阅读(195)  评论(0编辑  收藏  举报