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 ...
随机推荐
- 创建和管理表【weber出品必属精品】
创建表 必须有 : 1. CREATE TABLE 的权限 SQL> conn /as sysdba 已连接. SQL> create user test default tablespa ...
- iOS学习笔记-CoreData
简介 CoreData提供了对象关系映射(ORM)功能,从效果上说就是创建了一个"虚拟对象数据库",也可以把它看作一个综合的数据库管理库. NSManagedObjectConte ...
- 4 常量类--Map常量
public static final HashMap<String, String> ETL_SOURCE_INPUTTYPE_MAP = new HashMap<String, ...
- 为什么要用Math.sqrt(i)方法
java 练习题 判断 101-200 之间有多少个素数,并输出所有素数 public class Prime { public static int count = 0; public static ...
- 线段树练习 codevs 1080
/* codevs 1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 一行N个方格,开 ...
- eclipse 将文件夹作为sourcefolder
文件夹---右键
- mac/unix系统:C++实现一个端口扫描器
在比较早以前,我用过S扫描器, 以及大名鼎鼎的nmap扫描器, 可以快速扫描某个主机开放的端口, 今天使用C实现这样一个软件, 编译环境为Mac, 系统版本10.11.6: #include < ...
- Codeforces Round #281 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/493 A题 写完后就交了,然后WA了,又读了一遍题,没找出错误后就开始搞B题了,后来回头重做的时候才发现,球员被红牌罚下场后还可 ...
- Discuz!提取文章标签
<?php //强制使用字符集 @header('Content-Type: text/html; charset=gbk'); $subjectenc ='title'; //这是 ...
- cf B. Little Dima and Equation
http://codeforces.com/contest/460/problem/B import java.util.*; import java.math.*; public class Mai ...