android多线程下载-HttpURLConnection
来源:互联网 发布:js读取excel 编辑:程序博客网 时间:2024/05/19 03:43
android多线程下载-HttpURLConnection
private static String PATH = "http://192.168.37.2:8080/http/02.jpg"; public static void main(String[] args) throws Exception { URL url = new URL(PATH); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); int totalSize = conn.getContentLength(); int threadCount = 3; //计算每个字节下载的字节数 int blockSize = totalSize/3; //资源路径 String path = PATH; //目标路径 String target = "H://a.jpg"; for (int i = 1; i <= threadCount; i++) { int id = i; int startIndex = (id-1)*blockSize; int endIndex = -1; if (id==threadCount) { //3*3-1 endIndex = totalSize-1; }else { endIndex = id*blockSize-1; } DownloadThread downloadThread = new DownloadThread(id,startIndex,endIndex,path,target); downloadThread.start(); } } static class DownloadThread extends Thread{ private int id; private int startIndex; private int endIndex; private String path; private String target; public DownloadThread(int id, int startIndex, int endIndex,String path, String target) { super(); this.id = id; this.startIndex = startIndex; this.endIndex = endIndex; this.path = path; this.target = target; } @Override public void run() { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex); conn.connect(); int code = conn.getResponseCode(); if (206==code) { InputStream inputStream = conn.getInputStream(); File file = new File(target); RandomAccessFile raf = new RandomAccessFile(file ,"rwd"); raf.seek(startIndex); int len=-1; byte[] buffer = new byte[1024]; int total =0; while((len=inputStream.read(buffer))!=-1){ raf.write(buffer,0,len); total+=len; System.out.println(id+"下载了"+total); } System.out.println(id+"已完成"); raf.close(); } } catch (Exception e) { e.printStackTrace(); } } }
阅读全文
0 0
- Android HttpURLConnection 多线程下载
- android多线程下载-HttpURLConnection
- android HttpURLConnection实现多线程下载
- android多线程暂停下载-HttpURLConnection
- Android 使用HttpURLConnection实现多线程下载 笔记
- android中使用HttpURLConnection实现多线程下载
- Android-使用HttpURLConnection实现多线程下载
- HttpURLConnection多线程下载
- HttpURLConnection多线程下载
- HttpURLConnection实现多线程下载
- Java HttpUrlConnection多线程下载
- 多线程下载httpurlConnection
- Android开发_使用HttpURLConnection实现多线程下载
- Android移动开发-使用HttpURLConnection实现多线程的下载
- 使用HttpURLConnection实现多线程下载
- HttpURLConnection(四)-多线程下载
- HttpURLConnection实现多线程网络下载
- HttpUrlConnection实现多线程下载网络资源
- 阿里云服务器上建立网站的域名解析与配置(tomcat服务器)
- 牛客网(题目的回答大多数来自他人见解,少部分个人领悟)
- hashMap的底层实现原理
- vs踏过的坑
- JavaScript实现简单的循环式幻灯片
- android多线程下载-HttpURLConnection
- 安装完AndroidStudio,出现gradle project sync failed报错怎么办?
- 欢迎使用CSDN-markdown编辑器
- LightGBM参数介绍
- android多线程暂停下载-HttpURLConnection
- MT 205 COV Financial Institution Transfer Execution覆盖金融机构转账执行
- 新开淘宝店铺如何从0到1
- Java哈夫曼编码与解码
- 快捷使用电脑小习惯积累