A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 荒丶 中级黑马   /  2016-4-7 00:03  /  359 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.heima.javadownload;

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class ThreadDownLoad {

        private static int blockSize = 3000000;
        private static String path;

        public static void main(String[] args) {
                path = "http://188.188.1.10:8080/DownLoadServlet/huoshaodejimo.flac";
                try {
                        URL url = new URL(path);
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        if (conn.getResponseCode() == 200) {
                                int length = conn.getContentLength();

                                File file = new File(getFileName(path));
                                RandomAccessFile raf = new RandomAccessFile(file, "rw");
                                raf.setLength(length);
                                raf.close();

                                int threadCount = length / blockSize + 1;
                                for (int threadID = 0; threadID < threadCount; threadID++) {
                                        int startIndex = threadID * blockSize;
                                        int endIndex = (threadID + 1) * blockSize - 1;
                                        if (threadID == threadCount - 1) {
                                                endIndex = length - 1;
                                        }
                                        new Thread(new MyTask(threadID, startIndex, endIndex)).start();
                                        System.out.println(threadID + "开始下载");
                                }
                        }

                } catch (Exception e) {
                        e.printStackTrace();
                }

        }

        static class MyTask implements Runnable {
                private int threadID;
                private int startIndex;
                private int endIndex;
                private int currentPosition;

                public MyTask(int threadID, int startIndex, int endIndex) {
                        super();
                        this.threadID = threadID;
                        this.startIndex = startIndex;
                        this.endIndex = endIndex;
                        this.currentPosition = startIndex;
                }

                @Override
                public void run() {
                        try {
                                URL url = new URL(path);
                                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                                File file = new File(getFileName(path));

                                RandomAccessFile raf = new RandomAccessFile(file, "rw");

                                conn.setRequestProperty("range", "bytes=" + startIndex + "-" + endIndex);
                                raf.seek(startIndex);

                                if (conn.getResponseCode() == 206) {
                                        InputStream inputStream = conn.getInputStream();

                                        byte[] arr = new byte[8192];
                                        int len;
                                        while ((len = inputStream.read(arr)) != -1) {
                                                raf.write(arr, 0, len);

                                        }
                                        raf.close();
                                        inputStream.close();
                                }
                                System.out.println(threadID + "结束下载");
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                }

        }

        public static String getFileName(String path) {
                return path.substring(path.lastIndexOf("/") + 1);
        }

}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马