1、视图
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 8 <EditText 9 android:id="@+id/et_path" 10 android:layout_width="fill_parent" 11 android:layout_height="wrap_content" 12 android:text="http://202.96.2.126/android/360.exe" 13 android:hint="请输入地址" /> 14 15 <ProgressBar 16 android:id="@+id/pb" 17 style="?android:attr/progressBarStyleHorizontal" 18 android:layout_width="fill_parent" 19 android:layout_height="wrap_content" />" 20 21 <Button 22 android:onClick="downLoad" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:text="下载" 26 /> 27 28 29 30 </LinearLayout>
2、权限
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
3、多线程下载代码
1 package com.example.mymutildownload; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.InputStream; 6 import java.io.RandomAccessFile; 7 import java.net.HttpURLConnection; 8 import java.net.URL; 9 10 import android.os.Bundle; 11 import android.app.Activity; 12 import android.text.TextUtils; 13 import android.view.Menu; 14 import android.view.View; 15 import android.widget.EditText; 16 import android.widget.ProgressBar; 17 import android.widget.Toast; 18 19 public class MainActivity extends Activity { 20 21 private EditText et_path; 22 private ProgressBar pb; 23 private static int threadCount=3; 24 private static int liveThread = 3; 25 private static int currentLength = 0; 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 et_path = (EditText)findViewById(R.id.et_path); 31 pb = (ProgressBar)findViewById(R.id.pb); 32 } 33 34 35 public void downLoad(View view){ 36 final String path = et_path.getText().toString().trim(); 37 if(TextUtils.isEmpty(path)){ 38 Toast.makeText(this, "路径不能为空", 0).show(); 39 return; 40 } 41 42 new Thread(){ 43 public void run(){ 44 try { 45 URL url = new URL(path); 46 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 47 conn.setReadTimeout(5000); 48 conn.setRequestMethod("GET"); 49 int code = conn.getResponseCode(); 50 if(code == 200){ 51 int length = conn.getContentLength(); 52 System.out.println("文件的长度:" + length); 53 //生成一个文件 54 RandomAccessFile raf = new RandomAccessFile("/sdcard/360.exe", "rwd"); 55 raf.setLength(length); 56 raf.close(); 57 58 //设置进度条的长度 59 pb.setMax(length); 60 //文件大小 61 int blockSize = length / threadCount; 62 for(int threadId=1; threadId <= threadCount; threadId++){ 63 //下载的起始位置 64 int startIndex = (threadId - 1) * blockSize; 65 //结束位置 66 int endIndex = threadId * blockSize - 1; 67 if(threadId == threadCount){ 68 endIndex = length; 69 } 70 System.out.println("线程" + threadId + "的起始位置为:" + startIndex + " 结束位置为:" + endIndex); 71 //启动线程 72 new DownloadThread(threadId, startIndex, endIndex, path).start(); 73 } 74 } 75 } catch (Exception e) { 76 // TODO Auto-generated catch block 77 e.printStackTrace(); 78 System.out.println("链接失败"); 79 } 80 }; 81 }.start(); 82 } 83 84 85 public class DownloadThread extends Thread{ 86 private int threadId; 87 private int startIndex; 88 private int endIndex; 89 private String path; 90 public DownloadThread(int threadId, int startIndex, int endIndex, 91 String path) { 92 this.threadId = threadId; 93 this.startIndex = startIndex; 94 this.endIndex = endIndex; 95 this.path = path; 96 } 97 @Override 98 public void run(){ 99 try { 100 //判断文件是否存在 101 File tmpFile = new File("/sdcard/" + threadId + ".txt"); 102 if(tmpFile.exists()){ 103 FileInputStream fis = new FileInputStream(tmpFile); 104 byte[] buf = new byte[1024]; 105 fis.read(buf); 106 String downedLen = new String(buf).trim(); 107 startIndex = Integer.parseInt(downedLen); 108 fis.close(); 109 } 110 111 System.out.println("线程" + threadId + "aaaaaaaaaaaa的起始位置为:" + startIndex + " 结束位置为:" + endIndex); 112 URL url = new URL(path); 113 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 114 conn.setReadTimeout(5000); 115 conn.setRequestMethod("GET"); 116 conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); 117 int code = conn.getResponseCode(); 118 System.out.println("返回码是:" + code); 119 InputStream is = conn.getInputStream(); 120 RandomAccessFile raf = new RandomAccessFile("/sdcard/360.exe", "rwd"); 121 raf.seek(startIndex); 122 int len=0; 123 byte[] buffer = new byte[1024]; 124 //位置计算 125 int pos = startIndex; 126 while((len = is.read(buffer)) != -1){ 127 RandomAccessFile info = new RandomAccessFile("/sdcard/" + threadId + ".txt", "rw"); 128 raf.write(buffer, 0, len); 129 pos = pos + len; 130 //System.out.println("线程" + threadId + "已经下载的" + pos); 131 //设置进度条 132 synchronized(MainActivity.this){ 133 currentLength = currentLength + len; 134 pb.setProgress(currentLength); 135 } 136 137 info.write((pos + "").getBytes()); 138 info.close(); 139 } 140 is.close(); 141 raf.close(); 142 System.out.println("线程" + threadId + "下载完毕!"); 143 } catch (Exception e) { 144 // TODO Auto-generated catch block 145 e.printStackTrace(); 146 }finally{ 147 liveThread--; 148 if(liveThread == 0){ 149 for(int i=1; i <= threadCount; i++){ 150 File delFile = new File("/sdcard/" + i + ".txt"); 151 delFile.delete(); 152 } 153 System.out.println("删除了文件"); 154 } 155 } 156 157 } 158 } 159 @Override 160 public boolean onCreateOptionsMenu(Menu menu) { 161 // Inflate the menu; this adds items to the action bar if it is present. 162 getMenuInflater().inflate(R.menu.main, menu); 163 return true; 164 } 165 166 }
时间: 2024-09-28 08:00:03