缘起:
存放在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文件名称。