public class MultiThreadDown {    static int ThreadCount = 4;        static String path = "http://192.168.1.104:8080/android/pdf.exe";    static long start = 0;    public static void main(String[] args) throws Exception {        URL url = new URL(path);        HttpURLConnection conn = (HttpURLConnection) url.openConnection();        conn.setReadTimeout(5000);        conn.setRequestMethod("GET");        conn.setConnectTimeout(5000);        if (conn.getResponseCode() == 200) {            // 1.先获取请求资源的大小            int length = conn.getContentLength();            File file = new File("m.mpg");            // 生成临时文件            RandomAccessFile raf = new RandomAccessFile(file, "rwd");            // 设置临时文件的大小            raf.setLength(length);            raf.close();            // 计算每个线程应该要下载多少个字节            int size = length / ThreadCount;            start = System.currentTimeMillis();            for (int i = 0; i < ThreadCount; i++) {                // 计算线程下载的开始位置和结束位置                int startIndex = i * size;                int endIndex = (i + 1) * size - 1;                // 如果是最后一个线程,那么结束位置写死                if (i == (ThreadCount - 1)) {                    endIndex = length - 1;                }                new DownThread(startIndex, endIndex, i).start();            }        }    }}class DownThread extends Thread {    int startIndex;    int endIndex;    int threadId;    public DownThread(int startIndex, int endIndex, int threadId) {        super();        this.startIndex = startIndex;        this.endIndex = endIndex;        this.threadId = threadId;    }    @Override    public void run() {        // 再次发送HTTP请求,下载源文件        try {            URL url = new URL(MultiThreadDown.path);            HttpURLConnection conn = (HttpURLConnection) url.openConnection();            conn.setReadTimeout(5000);            conn.setRequestMethod("GET");            conn.setConnectTimeout(5000);            // 设置请求的数据的区间            conn.setRequestProperty("Range", "bytes=" + startIndex + "-"                    + endIndex);            // 请求部分数据,响应码为206            if (conn.getResponseCode() == 206) {                // 此时只有 1/threadcount数据                InputStream is = conn.getInputStream();                byte[] b = new byte[1024];                int len = 0;                long total = 0;                File file = new File("m.mpg");                RandomAccessFile raf = new RandomAccessFile(file, "rwd");                // 把文件的写入位置移动至startIndex                raf.seek(startIndex);                while ((len = is.read(b)) != -1) {                    // 每次读取流里的数据写入临时文件                    raf.write(b, 0, len);                    total += len;                }            }        } catch (IOException e) {            e.printStackTrace();        }    }}  

更多相关文章

  1. Android SQLite数据库相关操作
  2. android 自带的contacts2.db数据库表结构脚本
  3. android 两个apk之间恭喜数据SharedPreferences
  4. Android Sqlite数据库中判断某个表是否存在的SQL语句
  5. Android JSON数据的解析与封装小Demo
  6. Android中使用SQLite数据库详解
  7. Android中的线程模型
  8. 2010-03-03 传智播客—Android(五)数据存储之五网络数据交互

随机推荐

  1. 2011.06.03——— android 1.6 launcher
  2. Android启动流程、app启动原理
  3. android 使用MediaCodec 编解码总结(最全)
  4. 【Android】通知的实现
  5. Android上下TabHost设置及Did you forget
  6. android中调用指定浏览器
  7. 解析Android消息处理机制 ——HandlerThr
  8. Android(安卓)的网络编程(7)-检测网络是
  9. Android下拉刷新上拉加载控件,对所有View
  10. Android 直连SQL