單一檔案的斷點續傳

2021-08-03 23:30:11 字數 4258 閱讀 6011

private

int id;

private string url;

private string filename;

private

int length;

private

int finished;

threadinfo類是記錄執行緒資訊

private

int id;

private string url;

private

int start;

private

int end;

private

int finished;

dbhelper建立我的資料庫,單例模式

private

static

final string db_name="download.db";

public

static dbhelper helper;

private

static string sql_create="create table if not exists thread_info(_id integer primary key autoincrement,thread_id integer," +

"url text,start integer,end integer,finished integer)";

private

static string sql_drop="drop table if exists thread_info";

public

static

synchronized dbhelper getinstance(context mcontext)

return helper;

}private

dbhelper(context context, string name, sqlitedatabase.curso***ctory factory, int version)

@override

public

void

oncreate(sqlitedatabase db)

@override

public

void

onupgrade(sqlitedatabase db, int oldversion, int newversion)

**資料訪問介面的實現

* created by administrator on 2017/7/19.

*/public

class

threaddaoimp

implements

threaddao

@override

public

void

insertthread(threadinfos threadinfos) );

db.close();

}@override

public

void

deletethread(string url, int thredid) );

db.close();

}@override

public

void

updatethread(string url, int threadid, int finished) );

db.close();

}@override

public listgetthreads(string url) );

while (cursor.movetonext())

cursor.close();

db.close();

return list;

}@override

public

boolean

i***ists(string url, int threadid) );

boolean exists=cursor.movetonext();

cursor.close();

db.close();

return exists;

}} * created by administrator on 2017/7/19.

*/public

class

downloadtask

public

void

download()

else}}

class downloadthread extends thread

@override

public

void

run()

randomaccessfile raf=null;

try

if (system.currenttimemillis()-time>300)

if (ispaused)

}threaddaoimp.deletethread(threadinfos.geturl(),threadinfos.getid());

}} catch (malformedurlexception e) catch (ioexception e) finally

} catch (ioexception e) }}

}}/**資料庫訪問介面

* created by administrator on 2017/7/19.

*/public

inte***ce

threaddao

/** * created by administrator on 2017/7/19.

*/public

class

downloadservice

extends

service}};

@override

public

intonstartcommand(intent intent, int flags, int startid)

else

if (action_stop.equals(intent.getaction()))

}return

super.onstartcommand(intent, flags, startid);

}@nullable

@override

public ibinder onbind(intent intent)

class initthread extends thread

@override

public

void

run()

file dir=new file(download_path);

if (!dir.exists())

//在本地建立檔案

file file=new file(dir,fileinfos.getfilename());

raf = new randomaccessfile(file, "rwd");

//設定檔案長度

raf.setlength(length);

}fileinfos.setlength(length);

message message = handler.obtainmessage();

message.what=msg_init;

message.obj=fileinfos;

handler.sendmessage(message);

} catch (malformedurlexception e) catch (ioexception e) finally

} catch (ioexception e) }}

}}public

class

mainactivity

extends

implements

view.onclicklistener

private

void

initdata()

private

void

initfileinfo()

private

void

initlistener()

@override

public

void

onclick(view v)

}@override

protected

void

ondestroy()

class mybroadcastreceiver extends broadcastreceiver}}

}}這裡寫**片

HTTP檔案斷點續傳的原理

我一細想,這個問題壓根不需要通過改變現有介面提供更多的資料來做.下面從原理實現上簡單說下 關鍵點 對於斷點續傳,關鍵點是兩個 檔案變化感知 前置業務介面方案 對於關鍵點1,對於決定大部分產品的業務場景,可以通過前置業務介面解決 這裡簡單介紹一下 htpp 標準etag方案 etag原理 如果url上...

Go語言實現檔案的斷點續傳

注釋在 中 package main import fmt io log os strconv strings func main 寫入目標檔案 n3,err file2.write data n2 讀取總量相加 total n3 將複製的總量,儲存在臨時檔案中,從頭開始寫 file3.seek 0...

qt程式打包成單一檔案EXE 一

平台 qt5.7 n.1 把ico放到專案同目錄下 idi icon1 icon discardable n.3 在專案檔案.pro檔案裡加入下面內容 二 查詢所需dll qt 官方開發環境使用的動態鏈結庫方式,在發布生成的exe程式時,需要複製一大堆 dll,如果自己去複製dll,很可能丟三落四,...