实现异步多线程下载文件
来源:互联网 发布:矩阵化为行最简形 编辑:程序博客网 时间:2024/05/17 07:02
我是基于一名大神的思路写的,如果想看更多内容,下面大神的blog地址
http://blog.csdn.net/shimiso/article/details/6763664
xml文件
<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"> <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progressbar1" style="?android:attr/progressBarStyleHorizontal"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="textview1" android:id="@+id/textview1"/> <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progressbar2" style="?android:attr/progressBarStyleHorizontal"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="textview1" android:id="@+id/textview2"/></LinearLayout>
Activity
public class MainActivity extends AppCompatActivity { private TextView textView1,textView2; private ProgressBar progressBar1,progressBar2; //文件存储地址 private String downLoadingpath= Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator; //下载地址 private String downloadingFile1="http://gongxue.cn/yingyinkuaiche/UploadFiles_9323/201008/2010082909434077.mp3"; private String downloadingFile2="http://gongxue.cn/yingyinkuaiche/UploadFiles_9323/201008/2010082909434077.mp3"; //声明已经读过的长度变量 private int hasRead=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1= (TextView) findViewById(R.id.textview1); textView2= (TextView) findViewById(R.id.textview2); progressBar1= (ProgressBar) findViewById(R.id.progressbar1); progressBar2= (ProgressBar) findViewById(R.id.progressbar2); downLoad(downloadingFile1,downLoadingpath,progressBar1,textView1); downLoad(downloadingFile2,downLoadingpath,progressBar2,textView2); } private void downLoad(String url,String targetPath,ProgressBar pb,TextView textView) { DownloadThread downloadThread=new DownloadThread(url,targetPath,pb,textView); downloadThread.start(); } //自定义Handler类处理线程消息 public class MyHandler extends Handler{ private ProgressBar pb; private TextView textView; public MyHandler(ProgressBar pb, TextView textView) { this.pb = pb; this.textView = textView; } @Override public void handleMessage(Message msg) { this.pb.setProgress(msg.arg1); this.textView.setText(msg.arg1+"%"); super.handleMessage(msg); } } public class DownloadThread extends Thread{ String url=""; private String targetPath=""; private int hasDownLoad=0; private int len=-1; private byte buffer[]=new byte[4*1024]; private int size=0; //下载速度 private int rate=0; private MyHandler myHandler=null; private Message msg=null; private ProgressBar pb=null; private TextView textView=null; public DownloadThread(String url, String targetPath, ProgressBar pb, TextView textView) { this.url = url; this.targetPath = targetPath; this.pb = pb; this.textView = textView; myHandler=new MyHandler(this.pb,this.textView); } @Override public void run() { String targetFileName=this.targetPath +this.url.substring(this.url.lastIndexOf("/") +1,this.url.length()); File downloadFile=new File(targetFileName); if (!downloadFile.exists()) { try { downloadFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } try { URL fileUrl=new URL(this.url); HttpURLConnection conn= (HttpURLConnection) fileUrl.openConnection(); //获取文件大小 size=conn.getContentLength(); InputStream is=conn.getInputStream(); OutputStream os=new FileOutputStream(targetFileName); while ((len=is.read(buffer))!=-1) { os.write(buffer); hasDownLoad+=len; rate=(hasDownLoad*100/size); msg=new Message(); msg.arg1=rate; myHandler.sendMessage(msg); Log.e("rate----------",rate+"%"); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { } } }}
这是最基础的写法,基于handler更新UI,使用httpurlconnection下载文件内容,然后进行文件保存操作。
就截取了一部分下载过程。
0 0
- 实现异步多线程下载文件
- 实现多线程下载文件
- 多线程实现文件下载
- AsyncTask异步实现文件下载
- AsyncTask异步实现文件下载
- 如何实现多线程下载文件
- Java实现多线程下载文件
- 实现多线程下载同一个文件
- 多线程断点文件下载实现
- 使用AsyncTask实现异步文件下载
- Android AsyncTask异步实现大文件下载
- JavaScript 用fetch 实现异步下载文件
- (J2SE)实现多线程文件分块下载
- Android进阶:实现多线程下载文件
- Android进阶:实现多线程下载文件
- java实现多线程下载网络文件
- Android进阶:实现多线程下载文件
- 采用多线程的方式实现文件下载
- struts2的工作流程和运行原理
- Groovy SQL 分页组件
- POJ 2653 线段相交问题
- 10.1-7 两个队列实现一个栈
- html空标签
- 实现异步多线程下载文件
- Bash shell位置参数
- 用Json实现PHP与JavaScript间数据交换
- Android 开发环境下载地址 -- 百度网盘 adt-bundle android-studio sdk adt 下载
- 共享个地图控件的样式
- 批处理打开和关闭oracle11g 服务
- java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
- 浅析_tmain()与main()的区别
- JAVA学习网站