基于HTTP协议的多线程下载实现思路

基于HTTP协议的多线程下载,实现思路如下:
1.通过URL实例获取HTTPConnection;
2.根据HTTPConnection获取文件大小,通过文件大小,设置的线程数,计算每一个现场下载文件的起止Byte数。
3.在每个线程里,通过connection.setRequestProperty设置获取文件的Byte的起止位置。
4.线程中,获取到下载文件后,先在本地创建临时文件,通过seek方法设置文件偏置点,然后将下载文件流write到临时文件。


代码实例:


    package com.zhang;

    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 MutileThreadDownload1 {
        /*线程数*/
        private static int threadCount = 30;
        /*区块大小*/
        private static long blockSize;

        public static void main(String[] args) {
            /*服务器下载文件的路径*/
            String path1 = "http://127.0.0.1:8090/1.avi";
            String path = "http://127.0.0.1:8090/1.avi";
            try {
                URL url = new URL(path);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                int code = connection.getResponseCode();
                if (code == 200) {
                    long size = connection.getContentLength();
                    System.out.println("需要下载的文件大小:" + size);
                    blockSize = size / threadCount;
                    // 1,在本地创建和服务器一样大小的空白文件

                    /*File file = new File("temp.exe");
                    RandomAccessFile raf = new RandomAccessFile(file, "rw");
                    raf.setLength(size);*/

                    /*开启若干线程,分别下载对应的资源*/
                    for (int i = 1; i <= threadCount; i++) {
                        System.out.println("开启线程" + i);
                        long startIndex = ((i - 1) * blockSize + 0);
                        long endIndex = blockSize * i - 1;
                        if (i == threadCount) {
                            /*
                             * 最后一个线程
                             * */
                            endIndex = size - 1;
                        }
                        new DownLoadThread(i, startIndex, endIndex, path).start();
                        System.out.println("下载的位置:" + startIndex + "~" + endIndex);

                    }
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    class DownLoadThread extends Thread {
        private int threadId;
        private long startIndex;
        private long endIndex;
        private String path;

        public DownLoadThread(int threadId, long startIndex, long endIndex, String path) {
            this.threadId = threadId;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
            this.path = path;
        }

        public void run() {
            try {
                URL url = new URL(path);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);
                // connection.setRequestProperty("Range", "bytes=" + startIndex +
                // "-" + endIndex);
                connection.setConnectTimeout(5000);
                int code = connection.getResponseCode();
                System.out.println("code" + code);

                InputStream is = connection.getInputStream();
                File file = new File("temp.avi");
                RandomAccessFile raf = new RandomAccessFile(file, "rw");
                raf.seek(startIndex);
                System.out.println("第" + threadId + "个线程开始写文件的位置:" + String.valueOf(startIndex));
                int len = 0;
                byte[] buffer = new byte[1024];
                while ((len = is.read(buffer)) != -1) {
                    raf.write(buffer, 0, len);
                }
                is.close();
                raf.close();
                System.out.println("线程" + threadId + "下载完毕");
                if (code / 100 == 2) {

                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

你可能感兴趣的:(J2SE)