下载apk

void downFile(final String url) {
pBar.show();
new Thread() {
public void run() {

HttpClient client = new DefaultHttpClient();
// params[0]代表连接的url
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;

if (is != null) {

File file = new File(getApplicationContext().getFilesDir().getAbsolutePath(), "jtb.apk");
openFileOutput("jtb.apk", MODE_WORLD_READABLE);
fileOutputStream = new FileOutputStream(file);

byte[] buf = new byte[1024];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
// baos.write(buf, 0, ch);
fileOutputStream.write(buf, 0, ch);
count += ch;
if (length > 0) {
}
Message message = Message.obtain();

message.arg2=10;
//pBar.setMessage("请稍候..."+count+"/"+length);
message.obj="请稍候..."+count+"/"+length;
messageHandler.sendMessage(message);
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
down();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}

posted @ 2013-12-12 16:44  xxdc  阅读(715)  评论(0编辑  收藏  举报