public
void
click(View v) {
path = et_path.getText().toString().trim();
String threadcount_str = et_threadcount.getText().toString().trim();
final
int
threadcount = Integer.parseInt(threadcount_str);
if
(TextUtils.isEmpty(path) || threadcount <
0
) {
Toast.makeText(getApplicationContext(),
"输入错误"
,
0
).show();
return
;
}
list_pbBars =
new
ArrayList<ProgressBar>();
ll_layout.removeAllViews();
for
(
int
i =
0
; i < threadcount; i++) {
View item_pb_View = View.inflate(getApplicationContext(),R.layout.item_progress,
null
);
ProgressBar pb_bar = (ProgressBar) item_pb_View.findViewById(R.id.item_pb);
list_pbBars.add(pb_bar);
ll_layout.addView(item_pb_View);
}
try
{
sourceFile =
new
File(path);
new
Thread(){
public
void
run()
{
System.out.println(
"xxx"
+Thread.currentThread().getName());
try
{
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(
"GET"
);
int
responseCode = conn.getResponseCode();
if
(responseCode ==
200
)
{
sourceFileLength = conn.getContentLength();
sourceFileName = sourceFile.getName();
clientFile =
new
RandomAccessFile(Environment.getExternalStorageDirectory() +
"/"
+ sourceFileName,
"rw"
);
clientFile.setLength(sourceFileLength);
runningThreadCount = threadcount;
int
blockSize = sourceFileLength / threadcount;
for
(
int
i =
0
; i < threadcount; i++) {
int
start_index = i * blockSize;
int
end_index = (i +
1
) * blockSize -
1
;
int
last_index = -
1
;
if
(i == threadcount -
1
)
{
end_index = sourceFileLength -
1
;
}
File recordIndexFile =
new
File(Environment.getExternalStorageDirectory().getPath() +
"/"
+i +
".txt"
);
if
(recordIndexFile.exists() && recordIndexFile.length() >
0
)
{
BufferedReader bufr =
new
BufferedReader(
new
FileReader(recordIndexFile));
last_index = Integer.parseInt(bufr.readLine());
bufr.close();
}
else
{
last_index = start_index;
}
list_pbBars.get(i).setMax((
int
)(end_index - start_index));
System.out.println(
"线程"
+i +
"::"
+ last_index +
"----"
+ end_index);
new
DownloadThread(i, start_index, end_index,last_index).start();
}
}
else
{
showToast(
"请求不到资源 "
);
return
;
}
}
catch
(Exception e) {
e.printStackTrace();
}
}
}.start();
}
catch
(Exception e) {
e.printStackTrace();
}
}
class
DownloadThread
extends
Thread {
private
int
threadID;
private
int
start_index, end_index,last_index;
private
int
total =
0
;
public
DownloadThread(
int
threadID,
int
start_index,
int
end_index,
int
last_index) {
this
.threadID = threadID;
this
.start_index = start_index;
this
.end_index = end_index;
this
.last_index = last_index;
this
.setName(
"线程"
+ threadID);
}
public
void
run() {
try
{
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(
"GET"
);
conn.setRequestProperty(
"Range"
,
"bytes="
+ last_index +
"-"
+ end_index);
int
responseCode = conn.getResponseCode();
if
(responseCode ==
206
)
{
InputStream serverIn = conn.getInputStream();
RandomAccessFile myClientFile =
new
RandomAccessFile(
Environment.getExternalStorageDirectory() +
"/"
+ sourceFileName,
"rwd"
);
myClientFile.seek(last_index);
int
len =
0
;
byte
[] buf =
new
byte
[
1024
*
1024
];
while
((len = serverIn.read(buf))!= -
1
)
{
myClientFile.write(buf,
0
,len);
total += len;
int
current_index = last_index + total;
/***注意读取文件位置的流一定要用RandomAccessFile,因为它可以直接同步到缓存(磁盘上)。***/
RandomAccessFile raffAccessFile =
new
RandomAccessFile(Environment.getExternalStorageDirectory().getPath()+
"/"
+threadID+
".txt"
,
"rwd"
);
raffAccessFile.write(String.valueOf(current_index).getBytes());
raffAccessFile.close();
list_pbBars.get(threadID).setProgress((
int
)(total + last_index - start_index));
}
myClientFile.close();
serverIn.close();
synchronized
(DownloadThread.
class
) {
runningThreadCount--;
if
(runningThreadCount ==
0
)
{
Toast.makeText(getApplicationContext(),
"文件下载完毕"
,
0
).show();
}
}
}
else
{
showToast(
"服务器忙"
);
}
}
catch
(Exception e) {
e.printStackTrace();
}
}
}