java

位置:IT落伍者 >> java >> 浏览文章

JAVA编程语言开发下载文件


发布日期:2018年11月03日
 
JAVA编程语言开发下载文件

[java]

/**

* 下载文件

* @param url

*/

private void downFile(final String url) {

mProgressDialog = new ProgressDialog(activity)

mProgressDialogsetProgressStyle(ProgressDialogSTYLE_SPINNER)

mProgressDialogsetMessage(请等待…

mProgressDialogshow()

new Thread() {

@Override

public void run() {

DefaultHttpClient client = new DefaultHttpClient()

HttpGet get = new HttpGet(url)

HttpResponse response;

try {

response = clientexecute(get)

HttpEntity entity = responsegetEntity()

long length = entitygetContentLength()

InputStream is = entitygetContent()

FileOutputStream fileOutputStream = null;

if (is != null) {

File file = new File(PATH + software + apk

fileOutputStream = new FileOutputStream(file)

byte[] buf = new byte[];

int ch = ;

int count = ;

while ((ch = isread(buf)) != ) {

// baoswrite(buf ch)

fileOutputStreamwrite(buf ch)

count += ch;

if (length > ) {

}

}

}

fileOutputStreamflush()

if (fileOutputStream != null) {

fileOutputStreamclose()

}

update()

} catch (ClientProtocolException e) {

eprintStackTrace()

} catch (IOException e) {

eprintStackTrace()

}

}

}start()

}

private void update() {

//Logv(update update

Intent intent = new Intent(IntentACTION_VIEW)

intentsetDataAndType(

UrifromFile(new File(file:// + PATH + software + apk))

application/vndandroidpackagearchive

activitystartActivity(intent)

}

               

上一篇:JAVA中HashMap(哈希表)的使用(List)方法

下一篇:发现Java虚拟机内存洩露问题