Android基于HttpUrlConnection类的文件下载
/**
* get方法的文件下载
* <p>
* 特别说明 android中的progressBar是google唯一的做了处理的可以在子线程中更新UI的控件
*
* @param path
*/
private void httpDown(final String path) {
new Thread() {
@Override
public void run() {
URL url;
HttpURLConnection connection;
try {
//统一资源
url = new URL(path);
//打开链接
connection = (HttpURLConnection) url.openConnection();
//设置链接超时
connection.setConnectTimeout();
//设置允许得到服务器的输入流,默认为true可以不用设置
connection.setDoInput(true);
//设置允许向服务器写入数据,一般get方法不会设置,大多用在post方法,默认为false
connection.setDoOutput(true);//此处只是为了方法说明
//设置请求方法
connection.setRequestMethod("GET");
//设置请求的字符编码
connection.setRequestProperty("Charset", "utf-8");
//设置connection打开链接资源
connection.connect(); //得到链接地址中的file路径
String urlFilePath = connection.getURL().getFile();
//得到url地址总文件名 file的separatorChar参数表示文件分离符
String fileName = urlFilePath.substring(urlFilePath.lastIndexOf(File.separatorChar) + );
//创建一个文件对象用于存储下载的文件 此次的getFilesDir()方法只有在继承至Context类的类中
// 可以直接调用其他类中必须通过Context对象才能调用,得到的是内部存储中此应用包名下的文件路径
//如果使用外部存储的话需要添加文件读写权限,5.0以上的系统需要动态获取权限 此处不在不做过多说明。
File file = new File(getFilesDir(), fileName);
//创建一个文件输出流
FileOutputStream outputStream = new FileOutputStream(file); //得到链接的响应码 200为成功
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
//得到服务器响应的输入流
InputStream inputStream = connection.getInputStream();
//获取请求的内容总长度
int contentLength = connection.getContentLength(); //设置progressBar的Max
mPb.setMax(contentLength); //创建缓冲输入流对象,相对于inputStream效率要高一些
BufferedInputStream bfi = new BufferedInputStream(inputStream);
//此处的len表示每次循环读取的内容长度
int len;
//已经读取的总长度
int totle = ;
//bytes是用于存储每次读取出来的内容
byte[] bytes = new byte[];
while ((len = bfi.read(bytes)) != -) {
//每次读取完了都将len累加在totle里
totle += len;
//每次读取的都更新一次progressBar
mPb.setProgress(totle);
//通过文件输出流写入从服务器中读取的数据
outputStream.write(bytes, , len);
}
//关闭打开的流对象
outputStream.close();
inputStream.close();
bfi.close(); runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "下载完成!", Toast.LENGTH_SHORT).show();
}
});
} } catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
有不清楚的地方欢迎各位朋友们留言
Android基于HttpUrlConnection类的文件下载的更多相关文章
- Android的HttpUrlConnection类的GET和POST请求
/** * get方法使用 */ private void httpGet() { new Thread() { @Override public void run() { //此处的LOGIN是请求 ...
- 我的Android进阶之旅------>Android基于HTTP协议的多线程断点下载器的实现
一.首先写这篇文章之前,要了解实现该Android多线程断点下载器的几个知识点 1.多线程下载的原理,如下图所示 注意:由于Android移动设备和PC机的处理器还是不能相比,所以开辟的子线程建议不要 ...
- Android使用HttpURLConnection通过POST方式发送java序列化对象
使用HttpURLConnection类不仅可以向WebService发送字符串,还可以发送序列化的java对象,实现Android手机和服务器之间的数据交互. Android端代码: public ...
- 七、Android学习第六天——SQLite与文件下载(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 七.Android学习第六天——SQLite与文件下载 SQLite SQ ...
- 53. Android常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...
- Android基于XMPP的即时通讯2-文件传输
本文是在上一篇博文Android基于XMPP的即时通讯1-基本对话的基础上,添加新的功能,文件传输 1.初始化文件传输管理类 public static FileTransferManager get ...
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
Android 高手进阶(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处:http://blog.csdn.net/xiaanming/article/detail ...
- (转) Android的Window类
Android的Window类 2011-03-25 10:02 by Keis, 110 visits, 网摘, 收藏, 编辑 Android的Window类(一) Android的GUI层并不复 ...
- Android中Cursor类的概念和用法
http://blog.sina.com.cn/s/blog_618199e60101fskp.html 使用过 SQLite数据库的童鞋对 Cursor 应该不陌生,加深自己和大家对Android ...
随机推荐
- 统计单词个数(codevs 1040)
题目描述 Description 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k<= ...
- Sliding Window(滑动窗口)
Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 58002 Accepted: 16616 Case Time Limi ...
- Android GIS开发系列-- 入门季(6)GraphicsLayer添加文字与图片标签
一.GraphicsLayer添加图片 GraphicLayer添加图片Graphic,要用到PictureMarkerSymbol,也是样式的一种.添加代码如下: Drawable drawable ...
- postgresql备份和恢复
备份: pg_dump -d m3vg -h localhost -p 5432 -U delta -W -f 1024.dump -F tar 恢复: pg_restore -h localhost ...
- A Taxonomy for Performance
A Taxonomy for Performance In this section, we introduce some basic performance metrics. These provi ...
- 【Android】资源系列(一) -- 国际化(多语言)
1.Android 中要实现国际化比較简单. 字符串国际化:仅仅要在 res 目录下新建相应语言的 values 目录就好了. 如.英语环境下的.目录命名为:values-en ...
- Android自己定义提示框
在开发中,假设感觉系统自带的提示框不好看,开发人员能够自定义提示框的样式.主要是继承Dialog 程序文件夹结构 关键代码 package com.dzt.custom.dialog; import ...
- oc75--不可变字典NSDictionary
// // main.m // NSDictionary // // #import <Foundation/Foundation.h> int main(int argc, const ...
- MBEEWALK - Bee Walk
A bee larva living in a hexagonal cell of a large honey comb decides to creep for a walk. In each “s ...
- Linux设备驱动模型【转】
本文转载自:http://blog.csdn.net/xiahouzuoxin/article/details/8943863 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+ ...