熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

JAVA編程語言開發下載文件

2022-06-13   來源: 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)

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/27089.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.