HttpURLConnection 下载代码
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 下载代码的更多相关文章
- PHP 文件限速下载代码
php 文件限速下载代码 <?php include("DBDA.class.php"); $db = new DBDA(); $bs = $_SERVER["QU ...
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
使用HttpURLConnection下载文件时经常会出现 java.io.FileNotFoundException文件找不到异常,下面介绍下解决办法 首先设置tomcat对get数据的编码:con ...
- 从git上下载代码并导入eclipse
主要分为两步: 1.先从git下载代码到本地git仓库 2.eclipse import导入存在的maven项目
- axis1,xfire,jUnit 测试案列+开Web Service开发指南+axis1.jar下载 代码
axis1,xfire,jUnit 测试案列+Web Service开发指南(中).pdf+axis1.jar下载 代码 项目和资源文档+jar 下载:http://download.csdn. ...
- ubuntu svn下载代码出错
ubuntu svn下载代码出错: svn: OPTIONS of 'https://server.domain.local/svn/repo': SSL handshake failed: SSL ...
- gerrit设置非小组成员禁止下载代码
对gerrit有所了解的同学,都知道gerrit 是我们常用的一个来做代码审核的工具,其中的权限管理,是一个非常重要的环节,关于每个权限的使用范围,可以参考博客https://blog.csdn.ne ...
- sublime text3:下载代码格式化插件和汉化插件
1.从官网下载sublime text3 2.下载插件工具 A.使用Ctrl+`(Esc键下方)快捷键或者通过View->Show Console菜单打开命令行 将以下代码复制后粘贴,然后按En ...
- python之实现ftp上传下载代码(含错误处理)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之实现ftp上传下载代码(含错误处理) #http://www.cnblogs.com/kait ...
- python之模块ftplib(实现ftp上传下载代码)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ftplib(实现ftp上传下载代码) #需求:实现ftp上传下载代码(不含错误处理) f ...
随机推荐
- FineUI按钮控件
按钮的状态与大小 按钮有启用/禁用,按下/正常几种状态,对应的属性分别为Enabled.EnablePress.Pressed三个属性. 按钮有大中小三个尺寸,对应的属性为Size. 按钮上的图标 按 ...
- Cogs 12 运输问题2 (有上下界网络流)
#include <cstdlib> #include <algorithm> #include <cstring> #include <iostream&g ...
- adb安装和卸载apk的方式
昨天在使用adb卸载程序,结果死活卸载不了.我输入的命令和系统提示如下: D:\testApk>adb uninstall HelloWorld Failure 后来发现原来卸载程序时,只adb ...
- push方法的页面间跳转--
一,自定义动画写push方法-- 添加coreGraphics.framework框架 在CATransitionAnimation.h文件里面引入-- #import <QuartzCore/ ...
- iframe的操作
获取iframe的window,获取Iframe的document,获取父页面的window,某个环境是否iframe,动态创建iframe 这是demo.html,这个页用iframe嵌入了ifra ...
- 使用注解@Transient使表中没有此字段
注意,实体类中要使用org.springframework.data.annotation.Transient 在写实体类时发现有加@Transient注解的 加在属性声明上,但网上有加到get方法上 ...
- Big Event in HDU(HDU 1171 多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Hibernate整合Struts2时报错
今天在整合Hibernate和Struts2的时候遇到一个问题 总是出现各种异常,经过仔细检查,代码本身并没有问题, ----------------------------------------- ...
- IOS 推送原理
最近两天在研究ios的消息推送机制.研究这个东西,还是充满兴趣的. Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章 ...
- 优化C#程序的48种方法
一.用属性代替可访问的字段 1..NET数据绑定只支持数据绑定,使用属性可以获得数据绑定的好处: 2.在属性的get和set访问器重可使用lock添加多线程的支持. 二.readonly(运行时常量) ...