package com.dong.download.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author Administrator
*黑马 BASE 65课
*/
public class MainActivity extends Activity {
public static final int TEXTVIEW_PROGRESS = 2;
private Button b;
private ProgressBar bar;
private EditText ed;
// 和现在线程数相同,为了记录关闭线程时的计数器
static int deleteFileThread = 3;
private TextView tv;
static int threadCont = 3;
int currentProcess = 0;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1:
Toast.makeText(MainActivity.this, "服务器错误", 1).show();
break;
case TEXTVIEW_PROGRESS:
tv.setText(""+(bar.getProgress()*100/bar.getMax()));
break;
case 3:
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
bar = (ProgressBar) findViewById(R.id.pb);
ed = (EditText) findViewById(R.id.et_path);
tv = (TextView)findViewById(R.id.tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void download(View v) {
//String path = ed.getText().toString().trim();
new Thread() {
public void run() {
try {
String path = "http://192.168.1.100:8080/cbox2.4.0.9.exe";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (200 == code) {
int length = conn.getContentLength();
bar.setMax(length);
System.out.println("file length is" + length);
// 创建一个文件
RandomAccessFile raf = new RandomAccessFile(
"/sdcard/cbox2.4.0.9.exe", "rwd");
raf.setLength(length);
raf.close();
// 平均每一个线程的大小
int blockSize = length / threadCont;
for (int threadId = 1; threadId <= threadCont; threadId++) {
int startIndex = (threadId - 1) * blockSize;
int endIndex = threadId * blockSize - 1;
if (threadId == threadCont) {
endIndex = length;
}
System.out.println("Thread :" + threadId + "下载"
+ startIndex + "--->" + endIndex);
new DownThread(threadId, startIndex, endIndex, path)
.start();
}
} else {
System.out.println("server error");
}
} catch (Exception e) {
e.printStackTrace();
Message m = new Message();
m.what = 1;
handler.sendMessage(m);
}
}
}.start();
}
public class DownThread extends Thread {
private int ThreadId;
private int startIndex, endIndex;
private String path;
public DownThread(int threadId, int startIndex, int endIndex,
String path) {
this.ThreadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.path = path;
}
public void run() {
try {
File tempFile = new File("/sdcard/" + ThreadId + ".txt");
if (tempFile.exists() && tempFile.length() > 0) {
FileInputStream fis1 = new FileInputStream(ThreadId
+ ".txt");
byte[] temp = new byte[1024];
int leng = fis1.read(temp);
String downLen = new String(temp, 0, leng);
int iDownLoadLeng = Integer.parseInt(downLen);
int alreadDown = iDownLoadLeng-startIndex;
currentProcess+=alreadDown;
startIndex = iDownLoadLeng;
fis1.close();
}
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
+ endIndex);
System.out.println(ThreadId + " : 真实下载" + startIndex + "-"
+ endIndex);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();// 如果请求全部资源200代表OK,请求部分资源,206代表OK
System.out.println("code is " + code);
InputStream is = conn.getInputStream();
RandomAccessFile raf = new RandomAccessFile("/sdcard/"
+ "cbox2.4.0.9.exe", "rwd");
raf.seek(startIndex);
int len = 0;
byte[] buffer = new byte[1024];
int total = 0;
while ((len = is.read(buffer)) != -1) {
RandomAccessFile file = new RandomAccessFile("/sdcard/"
+ ThreadId + ".txt", "rwd");
total += len;
raf.write(buffer, 0, len);
System.out.println("Thread " + ThreadId + "total is :"
+ total);
file.write(("" + (total + startIndex)).getBytes());
synchronized (MainActivity.this) {
currentProcess+=len;
bar.setProgress(currentProcess);
Message m = Message.obtain();
m.what = TEXTVIEW_PROGRESS;
handler.sendMessage(m);
}
file.close();
}
raf.close();
System.out.println("thread" + ThreadId + "is downed");
} catch (Exception e) {
// TODO: handle exception
} finally {
finishThread();
}
}
private synchronized void finishThread() {
deleteFileThread--;
if (deleteFileThread == 0) {
for (int i = 1; i <= threadCont; i++) {
File delFile = new File("/sdcard/" + i + ".txt");
delFile.delete();
}
System.out.println("临时文件删除完毕");
}
}
}
}
android下载文件,支持续传
最新推荐文章于 2025-05-24 09:43:14 发布