public class DownloadActivity extends Activity implements OnClickListener{    private String path;    private static final int THREAD_NUM = 3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button button = new Button(this);        button.setOnClickListener(this);    }    @Override    public void onClick(View v) {        download(path);    }    private void download(String path) {        try {            URL url = new URL(path);            HttpURLConnection conn = (HttpURLConnection) url.openConnection();            conn.setDoInput(true);            conn.setDoOutput(true);            conn.setRequestMethod("GET");            conn.setConnectTimeout(5000);            if (conn.getResponseCode() == 200) {                int length = conn.getContentLength();                RandomAccessFile raf = new RandomAccessFile("down.exe", "rwd");                raf.setLength(length);                raf.close();                int blocksize = length / THREAD_NUM;                for (int i = 1; i <= THREAD_NUM; i++) {                    int start = (i-1) * blocksize;                    int end = i * blocksize - 1;                    if (i == THREAD_NUM) {                        end = length;                    }                    System.out.println("线程:"+i + "下载:" + start + "---->" + end);                    new DownloadThread(i, start, end, path).start();                }            }else {                System.out.println("服务器错误!");            }        } catch (Exception e) {            e.printStackTrace();        }    }    class DownloadThread extends Thread{        private int threadId;        private int start;        private int end;        private String path;        public DownloadThread(int threadId,int start,int end, String path){            this.threadId = threadId;            this.start = start;            this.end = end;            this.path = path;        }        @Override        public void run() {            try {                int tempLen=0;                //先读取临时文件                File temFile = new File(threadId+".txt");                if (temFile.exists()) {                    FileInputStream fis = new FileInputStream(temFile);                    byte[] buffer = new byte[1024];                    int len = fis.read(buffer);                    tempLen = Integer.parseInt(new String(buffer,0,len));                    start += tempLen;                }                URL url = new URL(path);                HttpURLConnection conn = (HttpURLConnection) url.openConnection();                conn.setRequestMethod("GET");                conn.setRequestProperty("Range", "bytes=" + start + "-" + end);                conn.setConnectTimeout(5000);                int total=0;                if (conn.getResponseCode() == 206) {                    InputStream inputStream = conn.getInputStream();                    RandomAccessFile raf = new RandomAccessFile("down.exe", "rwd");                    raf.seek(start);                    byte[] buffer = new byte[1024];                    int len = -1;                    while((len  = inputStream.read(buffer))!=-1){                        RandomAccessFile tempRaf = new RandomAccessFile(threadId+".txt", "rwd");                        raf.write(buffer, 0 , len);                        total+=len;                        System.out.println("线程:"+threadId + "下载total:" + total);                        tempRaf.write(((tempLen + total) + "").getBytes());                        tempRaf.close();                    }                    raf.close();                }            } catch (MalformedURLException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            }        }    }}

更多相关文章

  1. Ubuntu下 下载Android(安卓)源码(来自官网)
  2. android 读取指定路径数据库文件
  3. Android(安卓)通过handler和message在子线程里面去更新UI
  4. android访问webservice
  5. android计时器 message+handler; timer+timertask
  6. android socket
  7. Android文件下载进度条的实现
  8. Android(安卓)7.0版本升级解析包错误
  9. android之AsncTask异步任务使用

随机推荐

  1. android camera以时间来命名照片文件
  2. Android解决fragment重影问题
  3. Android Json 使用jsonschema2pojo生成.j
  4. JSBridge 实现机制
  5. android4.3环境搭建
  6. 在Android中销毁当前Fragment界面。
  7. Android(安卓)Studio NDK 入门demo
  8. (三)Android的ListView使用
  9. Android:APK签名
  10. Android实现渐显按钮的左右滑动效果