android实现多线程下载代码如下。
创新互联-专业网站定制、快速模板网站建设、高性价比迎泽网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式迎泽网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖迎泽地区。费用合理售后完善,10多年实体公司更值得信赖。
布局类
java代码如下
package cn.xhhkj.xhhkjtest;
import android.app.Activity;
import android.os.bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.progressbar;
import java.io.bufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends Activity implements OnClickListener{
private String path = "http://192.168.0.101/7z1900x64x3610.exe";
private int threadCount = 4;
private EditText et_url;
private EditText et_count;
private button btn_down;
private LinearLayout ll_progress;
private int blockSize;
@Override
protected void onCreate(bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_url = (EditText) findViewbyId(R.id.et_url);
et_count = (EditText) findViewbyId(R.id.et_count);
btn_down = (button) findViewbyId(R.id.btn_download);
ll_progress = (LinearLayout) findViewbyId(R.id.ll_progress);
btn_down.setOnClickListener(this);
}
@Override
public void onClick(View v) {
ll_progress.removeAllViews();
String temp = et_count.getText().toString().trim();
int count = Integer.parseInt(temp);
for(int i= 0;i View.inflate(getApplicationContext(), R.layout.item, ll_progress); } new Thread(){ public void run() { try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(10000); int code = connection.getResponseCode(); if(code==200){ int length = connection.getContentLength(); RandomAccessFile file = new RandomAccessFile(getFileName(path), "rw"); file.setLength(length); blockSize = length/threadCount; for(int i =0;i int startIndex = i*blockSize; int endIndex = (i+1)*blockSize-1; if(i == threadCount-1){ endIndex = length-1; } progressbar pb = (progressbar) ll_progress.getChildAt(i); pb.setMax(endIndex-startIndex); new DownLoadThread(startIndex, endIndex, i).start(); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); } private class DownLoadThread extends Thread{ private int startIndex; private int endIndex; private int threadID; private progressbar pb; public DownLoadThread(int startIndex, int endIndex, int threadID) { this.startIndex = startIndex; this.endIndex = endIndex; this.threadID = threadID; this.pb = (progressbar) ll_progress.getChildAt(threadID); } public void run() { try { File temp = new File(getFileName(path)+threadID+".log"); if(temp!=null && temp.length()>0){ FileInputStream fis = new FileInputStream(temp); bufferedReader reader = new bufferedReader(new InputStreamReader(fis)); String result = reader.readLine(); startIndex = Integer.parseInt(result); } URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(10000); connection.setRequestproperty("Range", "bytes="+startIndex+"-"+endIndex); if(connection.getResponseCode()==206){ System.out.println("线程"+threadID+"开始下载"+startIndex); InputStream inputStream = connection.getInputStream(); int len = -1; byte[] buffer = new byte[1024*500]; RandomAccessFile file = new RandomAccessFile(getFileName(path), "rw"); file.seek(startIndex); int count=0; while((len=inputStream.read(buffer))!=-1){ file.write(buffer, 0, len); count+=len; int position = count+startIndex; pb.setprogress(position-threadID*blockSize); RandomAccessFile tempFile = new RandomAccessFile(getFileName(path)+threadID+".log", "rwd"); tempFile.write(String.valueOf(position).getbytes()); } file.close(); inputStream.close(); System.out.println("线程"+threadID+"下载结束"); if(temp!=null){ temp.delete(); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private String getFileName(String path) { String[] result = path.split("/"); return getCacheDir().getAbsolutepath()+"/"+result[result.length-1]; } }
当前名称:android实现多线程下载实例
URL分享:http://cxhlcq.com/article/scdhes.html