android多线程暂停下载-HttpURLConnection
来源:互联网 发布:电动机控制模拟软件 编辑:程序博客网 时间:2024/05/22 17:31
android多线程暂停下载-HttpURLConnection
private EditText et_path; private LinearLayout ll_pbs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path); ll_pbs = (LinearLayout) findViewById(R.id.ll_pbs); } public void startDownload(View view){ //资源路径 final String path = et_path.getText().toString().trim(); new Thread(new Runnable() { @Override public void run() { URL url; try { url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); int totalSize = conn.getContentLength(); int threadCount = 3; //计算每个线程下载的字节数 int blockSize = totalSize/3; //目标路径 String target = Environment.getExternalStorageDirectory().getAbsolutePath()+"/bbb.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; } final ProgressBar pb = (ProgressBar) View.inflate(MainActivity.this, R.layout.pb, null); runOnUiThread(new Runnable() { @Override public void run() { ll_pbs.addView(pb); } }); DownloadThread downloadThread = new DownloadThread(id,startIndex,endIndex,path,target,pb); downloadThread.start(); } } catch (Exception e) { e.printStackTrace(); } } }).start(); } class DownloadThread extends Thread{ private int id; private int startIndex; private int endIndex; private String path; private String target; private File tempFile; private ProgressBar pb; private int newStartIndex; public DownloadThread(int id, int startIndex, int endIndex,String path, String target,ProgressBar pb) { super(); this.id = id; this.startIndex = startIndex; this.endIndex = endIndex; this.path = path; this.target = target; this.pb = pb; tempFile = new File(Environment.getExternalStorageDirectory(),"thread"+id+".tmp"); try { BufferedReader br = new BufferedReader(new FileReader(tempFile)); String readLine = br.readLine(); br.close(); newStartIndex = Integer.parseInt(readLine); } catch (Exception e) { newStartIndex = startIndex; } pb.setMax(endIndex-startIndex); } @Override public void run() { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Range", "bytes="+newStartIndex+"-"+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(newStartIndex); int len=-1; byte[] buffer = new byte[1024]; int total =0; while((len=inputStream.read(buffer))!=-1){ FileWriter fileWriter = new FileWriter(tempFile); raf.write(buffer,0,len); total+=len; fileWriter.write(""+(newStartIndex+total)); fileWriter.close(); //当前线程下载的总百分比 float process =(100.f* (total+newStartIndex-startIndex))/(endIndex-startIndex); System.out.println(id+"下载了"+process); pb.setProgress(total+newStartIndex-startIndex); } 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实现多线程下载
- Android 实现简易下载管理器 (暂停、断点续传、多线程下载)
- HttpURLConnection多线程下载
- HttpURLConnection多线程下载
- HttpURLConnection实现多线程下载
- Java HttpUrlConnection多线程下载
- 多线程下载httpurlConnection
- android 多线程断点下载,listview 模式 开始 暂停等功能
- android下多线程下载,断点续传,及暂停按钮
- Android开发_使用HttpURLConnection实现多线程下载
- Android移动开发-使用HttpURLConnection实现多线程的下载
- 使用HttpURLConnection实现多线程下载
- JavaScript实现简单的循环式幻灯片
- android多线程下载-HttpURLConnection
- 安装完AndroidStudio,出现gradle project sync failed报错怎么办?
- 欢迎使用CSDN-markdown编辑器
- LightGBM参数介绍
- android多线程暂停下载-HttpURLConnection
- MT 205 COV Financial Institution Transfer Execution覆盖金融机构转账执行
- 新开淘宝店铺如何从0到1
- Java哈夫曼编码与解码
- 快捷使用电脑小习惯积累
- 递归---案例(棋盘分割poj1191)
- 剑26-复杂链表的复制
- 【我的Java笔记】常用类_Arrays
- 文章标题