緣起:
存放在pc端的apk,如何下載到手機端並安裝,一種是通過IM通訊工具,或是複製到u盤上,或是ftp下載,還有就是通過雲盤安裝。但是以上方法都不是非常方便。
思路:
下面介紹一種思路:將apk文件存放在pc端的某一路徑下,然後生成html文件的鏈接併發布,在手機端使用百度瀏覽器旁的掃碼工具掃描並下載。
實現:
1)編寫生成文件夾遍歷並生成網頁文件列表以及發佈application的程序(參看tms sparkle的D:\riocomponents\TMS Sparkle v3.6.1\Demos\HttpFileServer樣例)
2)在上一步的基礎上實現二維碼生成功能。
uses
GY_RestClient, WaitBox;
procedure TForm1.FormCreate(Sender: TObject);
begin
ahost := 'http://122.228.166.158:101';
appid := 'G001';
appkey := 'h3476kjhl';
end;
var
Getstr, URL, method, uri, SaveToFile: string;
aStrlist: Tstringlist;
i: integer;
vs: TStringStream;
begin
TThread.CreateAnonymousThread(
procedure
begin
try
SaveToFile :=self.edBaseDir.Text + '/tempewm.jpg'; //GetHuanCunPath('temp')
aStrlist := Tstringlist.Create;
aStrlist.Add('str=http://'+self.IdIPWatch1.LocalIP+':2001/tms/business/sparkle/filelisting/');
method := 'POST'; // GET OR POST
uri := ServeAPIName + '/GetErWeiMaImage'; // 調用服務端的哪個函數通過這裏指定
GetURLandaStrlist(URL, method, uri, appkey, ahost, appid, aStrlist); // 組裝url和參數串並完成簽名驗證
vs := TStringStream.Create('', TEncoding.UTF8);
PostHTTPClient(URL, aStrlist, vs);
TThread.Synchronize(nil,
procedure
begin
if vs = nil then
if vs.Size < 40 then
else
begin
vs.SaveToFile(SaveToFile);
Image04.Picture.LoadFromFile(SaveToFile);
end;
end);
finally
aStrlist.Free;
vs.Free;
TThread.Synchronize(nil,
procedure
begin
end);
end;
end).Start;
將生成的二維碼鏈接到網頁上。
procedure TFileListingModule.ProcessDir(C: THttpServerContext; const Dir: string);
var
Writer: TStreamWriter;
RelativePath: string;
ParentPath: string;
FileName: string;
DisplayFileName: string;
SearchRec: TSearchRec;
begin
// this function provides an html listing all files in the directory
C.Response.StatusCode := 200;
C.Response.ContentType := 'text/html;charset=UTF8';
RelativePath := BuildRelativePath(GetRelativeSegments(C.Request.Uri));
ParentPath := BuildRelativePath(GetRelativeSegments(C.Request.Uri), 1);
// build the html response
Writer := TStreamWriter.Create(C.Response.Content, TEncoding.UTF8);
try
// html header with directory name
Writer.Write(Format('<html><head><title>Index of %s</title></head><body><h1>Index of %s</h1><img src="tempewm.jpg" alt="上海鮮花港 - 鬱金香" />',//這裏寫入二維碼的image鏈接
[RelativePath, RelativePath]));
Writer.Write('<pre> Name<hr>');
if FindFirst(TPath.Combine(Dir, '*'), faAnyFile, SearchRec) = 0 then
try
repeat
Filename := SearchRec.Name;
if FileName = '.' then Continue;
if (FileName = '..') and (RelativePath = '/') then Continue;
{$WARNINGS OFF}
if (SearchRec.Attr and faSymLink) <> 0 then Continue;
{$WARNINGS ON}
DisplayFileName := FileName;
if (SearchRec.Attr and faDirectory) <> 0 then
begin
Writer.Write('[DIR] ');
FileName := FileName + '/';
end
else
Writer.Write(' ');
Writer.Write(Format('<a href="%s">%s</a>'#13#10, [FileName, DisplayFileName]));
until FindNext(SearchRec) <> 0;
finally
FindClose(SearchRec);
end;
Writer.Write('<hr></pre><I>TMS Sparkle - FileListingServer demo</I></body></html>');
finally
Writer.Free;
end;
end;
3)發佈程序到指定服務器上。
4)打開網頁,使用手機端瀏覽器上的掃描工具掃碼,並點選相關的apk文件名稱。