android多執行緒暫停下載-HttpURLConnection
阿新 • • 發佈:2018-11-16
android多執行緒暫停下載-HttpURLConnection
private EditText et_path;
private LinearLayout ll_pbs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
ll_pbs = (LinearLayout) findViewById(R.id.ll_pbs);
}
public void startDownload(View view){
//資源路徑
final String path = et_path.getText().toString().trim();
new Thread(new Runnable() {
@Override
public void run() {
URL url;
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
int totalSize = conn.getContentLength();
int threadCount = 3;
//計算每個執行緒下載的位元組數
int blockSize = totalSize/3;
//目標路徑
String target = Environment.getExternalStorageDirectory().getAbsolutePath()+"/bbb.jpg";
for (int i = 1; i <= threadCount; i++) {
int id = i;
int startIndex = (id-1)*blockSize;
int endIndex = -1;
if (id==threadCount) {
//3*3-1
endIndex = totalSize-1;
}else {
endIndex = id*blockSize-1;
}
final ProgressBar pb = (ProgressBar) View.inflate(MainActivity.this, R.layout.pb, null);
runOnUiThread(new Runnable() {
@Override
public void run() {
ll_pbs.addView(pb);
}
});
DownloadThread downloadThread = new DownloadThread(id,startIndex,endIndex,path,target,pb);
downloadThread.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
class DownloadThread extends Thread{
private int id;
private int startIndex;
private int endIndex;
private String path;
private String target;
private File tempFile;
private ProgressBar pb;
private int newStartIndex;
public DownloadThread(int id, int startIndex, int endIndex,String path, String target,ProgressBar pb) {
super();
this.id = id;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.path = path;
this.target = target;
this.pb = pb;
tempFile = new File(Environment.getExternalStorageDirectory(),"thread"+id+".tmp");
try {
BufferedReader br = new BufferedReader(new FileReader(tempFile));
String readLine = br.readLine();
br.close();
newStartIndex = Integer.parseInt(readLine);
} catch (Exception e) {
newStartIndex = startIndex;
}
pb.setMax(endIndex-startIndex);
}
@Override
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Range", "bytes="+newStartIndex+"-"+endIndex);
conn.connect();
int code = conn.getResponseCode();
if (206==code) {
InputStream inputStream = conn.getInputStream();
File file = new File(target);
RandomAccessFile raf = new RandomAccessFile(file ,"rwd");
raf.seek(newStartIndex);
int len=-1;
byte[] buffer = new byte[1024];
int total =0;
while((len=inputStream.read(buffer))!=-1){
FileWriter fileWriter = new FileWriter(tempFile);
raf.write(buffer,0,len);
total+=len;
fileWriter.write(""+(newStartIndex+total));
fileWriter.close();
//當前執行緒下載的總百分比
float process =(100.f* (total+newStartIndex-startIndex))/(endIndex-startIndex);
System.out.println(id+"下載了"+process);
pb.setProgress(total+newStartIndex-startIndex);
}
System.out.println(id+"已完成");
raf.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}