首页 > 代码库 > 多线程下载

多线程下载

package 多线程下载;import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.ProtocolException;import java.net.URL;import java.net.URLConnection;/** *  * 1.获取到服务器资源文件的大小 *     conn.getContentLength() * 2.根据开启的线程的数量,把服务器的资源给等等成若干份 *     threadCount * 每个区块的大小: * int blocksize = length/threadcount * 3.随机访问文件流, * RandomAccessFile * 4.根据http协议 * conn. * 安卓客个户端“ * 创建 *  *  * @author Administrator * */public class MultileThread {     /**     * 线程的数量     * @param args     */    private static int  threadCount = 3;    /**     * 每个区块的大小     * @param args     */    private static long blockSize;    /**     * 正在运行的线程的数量     * @param args     */    private static int runningThreadCount;    private static HttpURLConnection conn;    public static void main(String[] args) {    try {        String path = "http://localhost:8080/";        //获取服务器文件路径        URL url = new URL(path);        conn = (HttpURLConnection) url.openConnection();        //设置连接参数        conn.setRequestMethod("GET");        //获取响应码        int code = conn.getResponseCode();        if(code == 206){//分段下载资源的响应码            long size = conn.getContentLengthLong();//得到服务端返回的文件大小            System.out.println("服务器文件大小:"+size);            blockSize = size /threadCount;                    //首先在本地创建一个大小跟服务器一模一样的空白文件            File file = new File("temp.exe");            RandomAccessFile raf = new RandomAccessFile(file,"rw");//rw模式,文件不存在会自动创建            //设置长度与服务器文件一致            raf.setLength(size);            //开启若干个子线程分别去下载对应的资源            runningThreadCount = threadCount;            for(int i = 1;i <= 3;i++){                long startIndex = (i - 1)*blockSize;//文件下载起始位置从0开始                long endIndex = i *blockSize-1;                if(i==threadCount){                    //最后一个线程                    endIndex = size -1;                }                System.out.println("开启线程:"+i +"    线程的位置:"+startIndex+"-"+endIndex);                //开启线程                            }        }        } catch (MalformedURLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }finally{        conn.disconnect();//取消连接    }}    private class MyThread extends Thread{        private long startIndex;        private long endIndex;        private int threadId;        private String path;                public MyThread(long startIndex, long endIndex, int threadId,                String path) {            super();            this.startIndex = startIndex;            this.endIndex = endIndex;            this.threadId = threadId;            this.path = path;        }        @Override        public void run() {            try {                //当前线程下载的总大小                int total = 0;                File positionFile = new File(threadId+".txt");                URL url = new URL("path");                HttpURLConnection conn = (HttpURLConnection) url.openConnection();                conn.setRequestMethod("GET");            /*    //从上一次的位置继续下载数据                if(positionFile.exists()&&positionFile.length()>0){//判断是否有记录                    }*/                                            } catch (MalformedURLException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch (ProtocolException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}

 

多线程下载