Android实现断点续传下载文件,网络编程


HTTP协议,我想上过计算机网络课程的童鞋肯定不陌生,但是谁又能说自己能把它实际运用上了,只有在实际项目开发的时候才会用到。Http算是Android网络中最常用到的网络协议了,客户端通过http通信与服务器进行数据交互,GET方法和POST方法想必是再熟悉不过了,本篇博客介绍一个比较实用的技术,断点续传下载,光看这个名字就感觉挺高大上,确实是,想实现它需要对http协议有一定的了解,并且对多线程机制比较熟悉,还有就是Android中异步更新UI的原理不能让程序出现卡顿的现象。

 

下载地址 : http://pan.baidu.com/s/1kT62nH1

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.wwj.download.db;
 
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
 
public class DBOpenHelper extends SQLiteOpenHelper {
private static final String DBNAME = "eric.db" ;
private static final int VERSION = 1 ;
 
public DBOpenHelper(Context context) {
super (context, DBNAME, null , VERSION);
}
 
@Override
public void onCreate(SQLiteDatabase db) {
// 创建filedownlog表
db.execSQL( "CREATE TABLE IF NOT EXISTS filedownlog (id integer primary key autoincrement, downpath varchar(100), threadid INTEGER, downlength INTEGER)" );
}
 
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL( "DROP TABLE IF EXISTS filedownlog" );
onCreate(db);
}
}

下载线程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.wwj.net.download;
 
import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
 
import android.util.Log;
 
public class DownloadThread extends Thread {
private static final String TAG = "DownloadThread" ;
private File saveFile;
private URL downUrl;
private int block;
/* 下载开始位置 */
private int threadId = - 1 ;
private int downLength;
private boolean finish = false ;
private FileDownloader downloader;
 
public DownloadThread(FileDownloader downloader, URL downUrl,
File saveFile, int block, int downLength, int threadId) {
this .downUrl = downUrl;
this .saveFile = saveFile;
this .block = block;
this .downloader = downloader;
this .threadId = threadId;
this .downLength = downLength;
}
 
@Override
public void run() {
if (downLength < block) { // 未下载完成
try {
HttpURLConnection http = (HttpURLConnection) downUrl
.openConnection();
http.setConnectTimeout( 5 * 1000 ); // 设置连接超时
http.setRequestMethod( "GET" ); // 设置请求方法,这里是“GET”
// 浏览器可接受的MIME类型
http.setRequestProperty(
"Accept" ,
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*" );
http.setRequestProperty( "Accept-Language" , "zh-CN" ); // 浏览器所希望的语言种类,当服务器能够提供一种以上的语言版本时要用到
http.setRequestProperty( "Referer" , downUrl.toString()); // 包含一个URL,用户从该URL代表的页面出发访问当前请求的页面。
http.setRequestProperty( "Charset" , "UTF-8" ); // 字符集
int startPos = block * (threadId - 1 ) + downLength; // 开始位置
int endPos = block * threadId - 1 ; // 结束位置
http.setRequestProperty( "Range" , "bytes=" + startPos + "-"
+ endPos); // 设置获取实体数据的范围
// 浏览器类型,如果Servlet返回的内容与浏览器类型有关则该值非常有用。
http.setRequestProperty(
"User-Agent" ,
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)" );
http.setRequestProperty( "Connection" , "Keep-Alive" ); // 设置为持久连接
 
// 得到输入流
InputStream inStream = http.getInputStream();
byte [] buffer = new byte [ 1024 ];
int offset = 0 ;
print( "Thread " + this .threadId
+ " start download from position " + startPos);
// 随机访问文件
RandomAccessFile threadfile = new RandomAccessFile(
this .saveFile, "rwd" );
// 定位到pos位置
threadfile.seek(startPos);
while (!downloader.getExit()
&& (offset = inStream.read(buffer, 0 , 1024 )) != - 1 ) {
// 写入文件
threadfile.write(buffer, 0 , offset);
downLength += offset; // 累加下载的大小
downloader.update( this .threadId, downLength); // 更新指定线程下载最后的位置
downloader.append(offset); // 累加已下载大小
}
threadfile.close();
inStream.close();
print( "Thread " + this .threadId + " download finish" );
this .finish = true ;
} catch (Exception e) {
this .downLength = - 1 ;
print( "Thread " + this .threadId + ":" + e);
}
}
}
 
private static void print(String msg) {
Log.i(TAG, msg);
}
 
/**
* 下载是否完成
*
* @return
*/
public boolean isFinish() {
return finish;
}
 
/**
* 已经下载的内容大小
*
* @return 如果返回值为-1,代表下载失败
*/
public long getDownLength() {
return downLength;
}
}

下载地址 : http://pan.baidu.com/s/1kT62nH1


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值