使用新的THttpClient多线程下载文件

最新的THttpClient组件替代了TIdHttp,调用的都是各个平台的原生api,天然支持https,不需要额外部署ssl的库。

  1. unit CMain;
  2.  
  3. interface
  4.  
  5. uses
  6. System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  7. FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
  8. FMX.TreeView, System.Generics.Collections, System.Net.HttpClient, System.Net.URLClient;
  9.  
  10. type
  11. TFormMain = class(TForm)
  12. tvDownload: TTreeView;
  13. tiRoot: TTreeViewItem;
  14. tiVideo: TTreeViewItem;
  15. tiEat: TTreeViewItem;
  16. tiFat: TTreeViewItem;
  17. tiNG: TTreeViewItem;
  18. procedure FormCreate(Sender: TObject);
  19. procedure tiClick(Sender: TObject);
  20. private
  21. { Private declarations }
  22. FDownloadMap:TDictionary<string,TTreeViewItem>;
  23. public
  24. { Public declarations }
  25. procedure ReceiveDataEvent(const Sender: TObject; AContentLength: Int64; AReadCount: Int64; var AAbort: Boolean);
  26. end;
  27.  
  28. var
  29. FormMain: TFormMain;
  30.  
  31. implementation
  32.  
  33. {$R *.fmx}
  34.  
  35. procedure TFormMain.FormCreate(Sender: TObject);
  36. begin
  37. //
  38. FDownloadMap:=TObjectDictionary<string,TTreeViewItem>.Create;
  39. tiEat.TagString:='https://www.520kxs.com/ikanbook.apk';
  40. tiFat.TagString:='http://www.flashavconverter.com/downloads/1.flv';
  41. tiNG.TagString:='http://www.flashavconverter.com/downloads/232423.flv';
  42. tvDownload.ExpandAll;
  43. end;
  44.  
  45. procedure TFormMain.ReceiveDataEvent(const Sender: TObject; AContentLength,
  46. AReadCount: Int64; var AAbort: Boolean);
  47. var
  48. http:IUrlRequest;
  49. ti:TTreeViewItem;
  50. org:String;
  51. rate:Double;
  52. fileSize:Integer;
  53. begin
  54. http:=(Sender as THttpRequest) as IUrlRequest;
  55. ti:=FDownloadMap[http.URL.ToString];
  56. fileSize:=AContentLength;
  57. org:=ti.Text.Split(['('])[0];
  58. rate:=AReadCount*100/fileSize;
  59.  
  60. TThread.Synchronize(nil, procedure
  61. begin
  62. ti.Text:=org+Format('(下载进度 %0.f)', [rate]);
  63. end);
  64. end;
  65.  
  66. procedure TFormMain.tiClick(Sender: TObject);
  67. var
  68. Url:string;
  69. ti:TTreeViewItem;
  70. http:THTTPClient;
  71. AStream:TMemoryStream;
  72. org:string;
  73. begin
  74. //
  75. ti:= (Sender as TTreeViewItem);
  76. Url:=ti.TagString;
  77. if (Url<>'') and (ti.TagObject=nil) then
  78. begin
  79. http:=THTTPClient.Create();
  80. http.OnReceiveData := ReceiveDataEvent;
  81. AStream:=TMemoryStream.Create;
  82. ti.TagObject:=http;
  83.  
  84. FDownloadMap.Add(url, ti);
  85.  
  86. //http.Tag:=NativeInt(ti);
  87. org:=ti.Text.Split(['('])[0];
  88.  
  89. TThread.CreateAnonymousThread( procedure
  90. begin
  91. try
  92. try
  93. http.Get(Url, AStream);
  94. except
  95. TThread.Synchronize(nil, procedure
  96. begin
  97. ti.Text:=org+'(下载失败)';
  98. end
  99. );
  100. Exit;
  101. end;
  102. TThread.Synchronize(nil, procedure
  103. begin
  104. ti.Text:=org+'(下载完成)';
  105. end
  106. );
  107.  
  108. AStream.SaveToFile('d:/temp/');
  109. finally
  110. AStream.Free;
  111. http.Free;
  112. end;
  113. end).Start;
  114. end;
  115. end;
  116.  
  117. end.