zoukankan      html  css  js  c++  java
  • 多线程下载

    多线程下载:

    package com.example.androidtest;
    
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    
    import android.os.Bundle;
    import android.os.Environment;
    import android.os.Handler;
    import android.os.Message;
    import android.app.Activity;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    
    public class MulThreadDownloadDemo extends Activity {
    
    	ProgressBar pb1,pb2;
    	TextView tv1,tv2;
    	String root = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator;
    	 private String downloadFile1 ="http://gongxue.cn/yingyinkuaiche/UploadFiles_9323/201008/2010082909434077.mp3";  
    	 private String downloadFile2 ="http://gongxue.cn/yingyinkuaiche/UploadFiles_9323/201008/2010082909434077.mp3";  
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_mul_thread);
    		
    		pb1 = (ProgressBar) findViewById(R.id.progressbar1);
    		pb2 = (ProgressBar) findViewById(R.id.progressbar2);
    		tv1=(TextView) findViewById(R.id.tv1);
    		tv2=(TextView) findViewById(R.id.tv2);
    		
    		download(downloadFile1,root,pb1,tv1);
    		download(downloadFile2,root,pb2,tv2);
    	}
    	
    	
    	public class MyHandlerDemo extends Handler{
    		ProgressBar progressBar;
    		TextView textview;
    		
    		public MyHandlerDemo(ProgressBar progressBar, TextView textview){
    			this.progressBar = progressBar;
    			this.textview = textview;
    			
    		}
    		@Override
    		public void handleMessage(Message msg) {
    			// TODO Auto-generated method stub
    			
    			this.progressBar.setProgress(msg.arg1);
    			this.textview.setText(msg.arg1+"%");
    			
    			super.handleMessage(msg);
    		}
    	}
    	private void download( String url,String targetPath,
    			ProgressBar pb, TextView tv) {
    		// TODO Auto-generated method stub
    		
    		DownloadThread dt = new DownloadThread(url,targetPath,pb,tv);
    		dt.start();
    	}
    	
    	public class DownloadThread extends Thread{
    		String url = "";
    		String targetPath = "";
    		ProgressBar pb = null;
    		TextView tv = null;
    		MyHandlerDemo myHandler = null;  
    		private int size = 0;
    		byte buffer[] = new byte[4*1024];
    		private int  len =-1;
    		int hasDownload = 0;  
    		int rate = 0;  
    		Message msg = null;  
    		public DownloadThread(String url, String targetPath, ProgressBar pb,
    				TextView tv) {
    			// TODO Auto-generated constructor stub
    			this.url = url;
    			this.targetPath = targetPath;
    			this.pb = pb;
    			this.tv = tv;
    			myHandler = new MyHandlerDemo(this.pb, this.tv);
    		}
    		@Override
    		public void run() {
    			// TODO Auto-generated method stub
    			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) {
    					// TODO Auto-generated catch block
    					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);
    					
    				}
    				
    			} catch (MalformedURLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    			
    		}
    		}
    		
    	}
    	
    	
    

    activity_mul_thread.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"
        
       >
       <ProgressBar 
           style="?android:attr/progressBarStyleHorizontal"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:id="@+id/progressbar1"
           
           />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv1"
            android:text="0%"
            android:textSize="25sp"
            
            />
        
        <ProgressBar 
           style="?android:attr/progressBarStyleHorizontal"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:id="@+id/progressbar2"
           
           />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv2"
            android:text="0%"
            android:textSize="25sp"
            />
    
    </LinearLayout>
    
  • 相关阅读:
    3、Nginx负载均衡实现的策略
    2、Nginx 是如何实现并发的?为什么 Nginx 不使用多线程?Nginx常见的优化手段有哪些?502错误可能原因有哪些?
    1、HTTP 的负载均衡?Nginx负载均衡
    用 Python 手写十大经典排序算法
    处理TypeError: testFunc() missing 1 required positional argument: 'self' -- 没有实例化对象的错误
    Socket技术详解
    MAC终端常用命令
    接口自动化测试框架 -- reudom
    如何在Pypi发布上传你自己的Python库
    Docker数据目录迁移解决方案
  • 原文地址:https://www.cnblogs.com/childhooding/p/4481070.html
Copyright © 2011-2022 走看看