android(25)(android下实现多线程断点下载)
2015-12-05 16:37
555 查看
1.布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入下载文件的路径" /> <EditText android:id="@+id/et_path" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://192.168.1.100:8080/" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入线程的数量" /> <EditText android:id="@+id/et_count" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number" android:text="3" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="downLoad" android:text="下载" /> <LinearLayout android:id="@+id/ll_container" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout> </LinearLayout> 进度条的布局: <?xml version="1.0" encoding="utf-8"?> <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/progressBar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 业务逻辑代码的实现: public class MainActivity extends Activity { protected static final int DOWNLOAD_ERROR = 1;//下载错误 private static final int THREAD_ERROR = 2;//线程数量错误 public static final int DWONLOAD_FINISH = 3;//下载完毕 private EditText et_path;//url路径 private EditText et_count;//线程数量 /** * 存放进度条的布局 */ private LinearLayout ll_container; /** * 进度条的集合 */ private List<ProgressBar> pbs; /** * android下的消息处理器,在主线程创建,才可以更新ui */ private Handler handler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case DOWNLOAD_ERROR: Toast.makeText(getApplicationContext(), "下载失败", 0).show(); break; case THREAD_ERROR: Toast.makeText(getApplicationContext(), "下载失败,请重试", 0).show(); break; case DWONLOAD_FINISH: Toast.makeText(getApplicationContext(), "下载完成", 0).show(); break; } }; }; /** * 线程的数量 */ private int threadCount = 3; /** * 每个下载区块的大小 */ private long blocksize; /** * 正在运行的线程的数量 */ private int runningThreadCount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path); et_count = (EditText) findViewById(R.id.et_count); ll_container = (LinearLayout) findViewById(R.id.ll_container); } /** * 下载按钮的点击事件 * @param view */ public void downLoad(View view){ //下载文件的路径 final String path = et_path.getText().toString().trim(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "对不起下载路径不能为空", 0).show(); return; } //开启线程的数量 String count = et_count.getText().toString().trim(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "对不起,线程数量不能为空", 0).show(); return; } threadCount = Integer.parseInt(count); //清空掉旧的进度条(线性布局清理掉所有的视图) ll_container.removeAllViews(); //在界面里面添加count个进度条 pbs = new ArrayList<ProgressBar>(); for(int j=0;j<threadCount;j++){ ProgressBar pb = (ProgressBar) View.inflate(this, R.layout.pb, null); ll_container.addView(pb);//把进度条添加到现行布局的容器中 pbs.add(pb);//把进度条添加到进度条集合中。 } Toast.makeText(this, "开始下载", 0).show(); new Thread(){ public void run() { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); if (code == 200) { long size = conn.getContentLength();// 得到服务端返回的文件的大小 System.out.println("服务器文件的大小:" + size); blocksize = size / threadCount; // 1.首先在本地创建一个大小跟服务器一模一样的空白文件。 File file = new File(Environment.getExternalStorageDirectory(),getFileName(path)); RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.setLength(size); // 2.开启若干个子线程分别去下载对应的资源。 runningThreadCount = threadCount; for (int i = 1; i <= threadCount; i++) { long startIndex = (i - 1) * blocksize; long endIndex = i * blocksize - 1; if (i == threadCount) { // 最后一个线程 endIndex = size - 1; } System.out.println("开启线程:" + i + "下载的位置:" + startIndex + "~" + endIndex); int threadSize = (int) (endIndex - startIndex); pbs.get(i-1).setMax(threadSize); new DownloadThread(path, i, startIndex, endIndex).start(); } } conn.disconnect(); } catch (Exception e) { e.printStackTrace(); Message msg = Message.obtain(); msg.what = DOWNLOAD_ERROR; handler.sendMessage(msg); } }; }.start(); } private class DownloadThread extends Thread { private int threadId; private long startIndex; private long endIndex; private String path; public DownloadThread(String path, int threadId, long startIndex, long endIndex) { this.path = path; this.threadId = threadId; this.startIndex = startIndex; this.endIndex = endIndex; } @Override public void run() { try { // 当前线程下载的总大小 int total = 0; //下载的进度信息存储到一个文本文件中。以线程id命名。 File positionFile = new File(Environment.getExternalStorageDirectory(),getFileName(path)+threadId + ".txt"); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod("GET"); // 接着从上一次的位置继续下载数据 if (positionFile.exists() && positionFile.length() > 0) {// 判断是否有记录 FileInputStream fis = new FileInputStream(positionFile); BufferedReader br = new BufferedReader( new InputStreamReader(fis)); // 获取当前线程上次下载的总大小是多少 String lasttotalstr = br.readLine(); int lastTotal = Integer.valueOf(lasttotalstr); System.out.println("上次线程" + threadId + "下载的总大小:" + lastTotal); startIndex += lastTotal; total += lastTotal;// 加上上次下载的总大小。 fis.close(); //存数据库。 //_id path threadid total } conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); System.out.println("code=" + code); InputStream is = conn.getInputStream(); File file = new File(Environment.getExternalStorageDirectory(),getFileName(path)); RandomAccessFile raf = new RandomAccessFile(file, "rw"); // 指定文件开始写的位置。 raf.seek(startIndex); System.out.println("第" + threadId + "个线程:写文件的开始位置:" + String.valueOf(startIndex)); int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { RandomAccessFile rf = new RandomAccessFile(positionFile, "rwd"); raf.write(buffer, 0, len); total += len; rf.write(String.valueOf(total).getBytes()); rf.close(); pbs.get(threadId-1).setProgress(total); } is.close(); raf.close(); } catch (Exception e) { e.printStackTrace(); Message msg = Message.obtain(); msg.what = THREAD_ERROR; handler.sendMessage(msg); } finally { // 只有所有的线程都下载完毕后 才可以删除记录文件。 synchronized (MainActivity.class) { System.out.println("线程" + threadId + "下载完毕了"); runningThreadCount--; if (runningThreadCount < 1) { System.out.println("所有的线程都工作完毕了。删除临时记录的文件"); for (int i = 1; i <= threadCount; i++) { File f = new File(Environment.getExternalStorageDirectory(),getFileName(path)+ i + ".txt"); System.out.println(f.delete()); } Message msg = Message.obtain(); msg.what = DWONLOAD_FINISH; handler.sendMessage(msg); } } } } } //http://192.168.1.100:8080/aa.exe //根据路径名称得到文件名称 private String getFileName(String path){ int start = path.lastIndexOf("/")+1; return path.substring(start); }
相关文章推荐
- Android控件之Gallery
- 【Material Design学习研究 一】个人对Material Design的理解和看法!
- Android控件之TimePicker、DatePicker
- Android SDK 开发范例大全 ---3.13置换文字颜色的机关
- android 自定义无限循环播放的viewPager。轮播ViewPager。实现循环播放 广告,主题内容,活动,新闻内容时。
- android studio 用git把项目推到bitbucket仓库
- Android 学习路线(参考)
- Android测试教程12--模拟读取的文件/数据库
- Android广播动态注册重复注册问题
- Android5.0推出的替换listView的控件recyclerview
- android makefile文件(Android.mk)组织结构解析
- android 世上actionbra最详细的介绍
- Android统计图表MPAndroidChart:为多条统计折线动态更新数据,以高温低温曲线为例【7】
- 释放Android的函数式能量(I):Kotlin语言的Lambda表达式
- Android开发者文档笔记(一)
- Android中Bitmap和Drawable
- android(24)(用java实现多线程断点下载)
- android下面的文案重用
- Android 布局 LinearLayout与RelativeLayout的布局属性
- ListView实现公告切换效果