private int downloadFile(final String apkurl, final String apkname) {
Log.e(LOGTAG, "downloadApkBackground..apkurl="+apkurl+"; filePath="+apkname);
File file = new File(apkname);
FileOutputStream out = null;
InputStream is = null;
int fileLength = 0;
long count = 0;
try{
URL url = new URL(apkurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(6*1000);
conn.setRequestMethod("GET");
conn.setDoOutput(true);
if(file.exists()){
count = file.length();
} conn.setRequestProperty("User-Agent", "NetFox");
conn.setRequestProperty("RANGE", "bytes=" + count + "-");
conn .setRequestProperty("Accept-Encoding", "identity");
out = new FileOutputStream(file, true);
is = conn.getInputStream();
fileLength = conn.getContentLength();
Log.e(LOGTAG, "downloadApkBackground...count="+count+"; fileLength="+fileLength); byte buf[] = new byte[4 * 1024];
int size = 0; while ((size = is.read(buf)) != -1) { //down and cached
try {
out.write(buf, 0, size);
count += size;
if(count >= fileLength){
return UPDATE_FAIL_WITH_SUCCESS;
} //Log.e(LOGTAG, "downloadApkBackground..count="+count);
//publishProgress(count, fileLength);
out.flush();
} catch (Exception e) {
e.printStackTrace();
mDownloadIsError = true;
return UPDATE_FAIL_WITH_FAIL_UNKONWN;
}
}
return UPDATE_FAIL_WITH_SUCCESS;
}catch (MalformedURLException e) {
mDownloadIsError = true;
e.printStackTrace();
}catch (IOException e) {
mDownloadIsError = true;
e.printStackTrace();
}finally{
try{
out.close();
is.close();
}
catch(Exception e){
mDownloadIsError = true;
e.printStackTrace();
}
} return UPDATE_FAIL_WITH_FAIL_UNKONWN;
}

HttpURLConnection 下载代码的更多相关文章

  1. PHP 文件限速下载代码

    php 文件限速下载代码 <?php include("DBDA.class.php"); $db = new DBDA(); $bs = $_SERVER["QU ...

  2. 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法

    使用HttpURLConnection下载文件时经常会出现 java.io.FileNotFoundException文件找不到异常,下面介绍下解决办法 首先设置tomcat对get数据的编码:con ...

  3. 从git上下载代码并导入eclipse

    主要分为两步: 1.先从git下载代码到本地git仓库 2.eclipse import导入存在的maven项目

  4. axis1,xfire,jUnit 测试案列+开Web Service开发指南+axis1.jar下载 代码

    axis1,xfire,jUnit 测试案列+Web Service开发指南(中).pdf+axis1.jar下载    代码 项目和资源文档+jar 下载:http://download.csdn. ...

  5. ubuntu svn下载代码出错

    ubuntu svn下载代码出错: svn: OPTIONS of 'https://server.domain.local/svn/repo': SSL handshake failed: SSL ...

  6. gerrit设置非小组成员禁止下载代码

    对gerrit有所了解的同学,都知道gerrit 是我们常用的一个来做代码审核的工具,其中的权限管理,是一个非常重要的环节,关于每个权限的使用范围,可以参考博客https://blog.csdn.ne ...

  7. sublime text3:下载代码格式化插件和汉化插件

    1.从官网下载sublime text3 2.下载插件工具 A.使用Ctrl+`(Esc键下方)快捷键或者通过View->Show Console菜单打开命令行 将以下代码复制后粘贴,然后按En ...

  8. python之实现ftp上传下载代码(含错误处理)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之实现ftp上传下载代码(含错误处理) #http://www.cnblogs.com/kait ...

  9. python之模块ftplib(实现ftp上传下载代码)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ftplib(实现ftp上传下载代码) #需求:实现ftp上传下载代码(不含错误处理) f ...

随机推荐

  1. Android下按钮的使用方法

    package com.hangsheng.button; import android.app.Activity; import android.os.Bundle; import android. ...

  2. C#图像处理(3):在图像上加条形码

    引入Aspose.BarCode用来生成条形码,方法如下: /// <summary> /// 生成条码 /// </summary> /// <param name=& ...

  3. C#属性和readonly类型

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. Emacs显示行号

    在配置.emacs文件中加上    (global-linum-mode t) 启动emacs后按     m-x  global-linum-mode 就可以显示行号,但是每次打开emacs,要重新 ...

  5. linux下启动和关闭网卡命令及DHCP上网

    ifup.ifdown:如果在 /etc/sysconfig/network-scripts里面的ifcfg-ethx等文件存在的话,就可以通过ifdown或ifup来实现网卡的开和关,例如:ifup ...

  6. VC中窗口ID,句柄,指针三者相互转换函数【转】

    ID--HANDLE--HWND三者之间的互相转换id->句柄        hWnd = ::GetDlgItem(hParentWnd,id);id->指针        CWnd:: ...

  7. 把int类型值转换成int数组(不通过string类型转换)

    只适合初学者 今天同事问了我不通过string类型把int类型值123589转换成int[]数组.我想了想于是写了出来,其实不难.看你小学数学学得好不好.言归正传. 先不说代码,举个列子就知道怎么玩了 ...

  8. 【转】嵌入式Linux学习笔记

    一  嵌入式系统定义: 应用于特定环境的硬件体系. 二  两样非常重要的能力: 1.  掌握各种新概念的能力 2.  调试的能力( 包括软件, 硬件 ) 三  需要的基础知识: 1.  操作系统理论基 ...

  9. 完美解决ListView 与 ScrollView 共存问题

    1:首先设置ListView的高度,在setAdapter之后调用此方法. public static void setListViewHeightBasedOnChildren(ListView l ...

  10. Tomcat 6.0.32 +Spring dbcp datasource关闭Tomcat出现严重异常

    异常如下: 信息: Pausing Coyote HTTP/ -- :: org.apache.catalina.core.StandardService stop 信息: Stopping serv ...