用delphi7实现IdFTP的ftp方式多线程下载源代码(0分)

  • 主题发起人 主题发起人 王思佳
  • 开始时间 开始时间

王思佳

Unregistered / Unconfirmed
GUEST, unregistred user!
敬请来自:sbzldlb的高手灾再指点(用delphi7实现IdFTP的ftp方式多线程下载源代码)
多谢了!
或其他高手帮帮忙,多谢了!!!!!!!!!
//接收文件
function TForm1.GetURLFileName(aURL: string): string;
var
i: integer;
s: string;
begin
//返回下载地址的文件名
s := aURL;
i := Pos('/', s);
while i <> 0do
//去掉&quot;/&quot;前面的内容剩下的就是文件名了
begin
Delete(s, 1, i);
i := Pos('/', s);
end;
Result := s;
end;

//得到文件大小
function TForm1.GetFileSize(aURL: string): integer;
var
FileSize : integer;
tStream: TFileStream;
FileName: String;
begin
//tStream.size := 0;
IdFTP1.StructureMount(aURL);********************************不知道用的对不对?
FileSize := IdFTP1.Response.ContentLength;(IdFTP1的属性不对)***********怎么得到(IdFTP1从aURL里下载的文件内容的大小)
//FileSize := IdFTP1.size(FileName);
//FileSize := IdFTP1.ContentLength(FileName);
IdFTP1.Abort;
Result := FileSize;
end;

//多线程下载
procedure TForm1.Button11Click(Sender: TObject);
var
m:integer;
begin
Showmessage('OK!主线程在执行,获得文件名并显示在Edit5中');
aURL := Edit4.Text;
//ftp方式下载地址
aFile := GetURLFileName(Edit4.Text);//得到文件名
xx:= StrToInt(Edit5.Text);
//输入的线程数
m:=1;
aFileSize := GetFileSize(aURL);
avg := trunc(aFileSize/xx);
try
GetThread();
while m<=xxdo
begin
MyThread[m].Resume;
//唤醒线程
m :=m+1;
end;
except
Showmessage('创建线程失败!');
Exit;
end;
end;

//开始下载前,将ProgressBar1的最大值设置为需要接收的数据大小.*******************
procedure TForm1.IdFTP1Workbegin
(Sender: TObject;
AWorkMode: TWorkMode;
const AWorkCountMax: Integer);
begin
AbortTransfer := False;
ProgressBar1.Max:=AWorkCountMax;
ProgressBar1.Min:=0;
ProgressBar1.Position:=0;
end;

//状态显示
procedure TForm1.IdFTP1Status(ASender: TObject;
const AStatus: TIdStatus;
const AStatusText: String);
begin
ListBox1.ItemIndex := ListBox1.Items.Add(AStatusText);
end;

// 多线程的产生
procedure TForm1.GetThread();
var
i ,start,last : integer;
FileName : String;
begin
i:=1;
while i<=xxdo
begin
if i=1 then
begin
start := 0;
last := avg*i;
end
else
start := avg*(i-1);
last := avg*i;
FileName:=aFile+IntToStr(i);
MyThread:=TThread1.create(aURL, aFile,FileName, false , i,start,last);
i :=i+1;
end;
end;

//构造函数
constructor TThread1.create(aURL, aFile,FileName: String;
bResume: Boolean ;Count,start,last:integer);
begin
inherited create(true);
FreeOnTerminate := true;
tURL := aURL;
tFile := aFile;
tCount := Count;
tResume := bResume;
tstart :=start;
tlast :=last;
temFileName:= FileName;
end;

//下载文件函数
procedure TThread1.DownLodeFile();
var
//ftp: TIdFTP;
TIdFTP1 : TIdFTP;
tStream: TFileStream;
begin
TIdFTP1 := TIdFTP.Create(nil);
Form1.IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应
if FileExists(temFileName) then
//如果文件已经存在
tStream := TFileStream.Create(temFileName, fmOpenWrite) else
tStream := TFileStream.Create(temFileName, fmCreate);
if tResume then
//续传方式
begin
exit;
end else
//覆盖或新建方式
begin
TIdFTP1.MaxLineLength := tstart;(不对)********************文件下载的开始位置用TIdFTP什么属性来设置?
TIdFTP1.MinLineLength := tlast;(不对)*********************文件下载的结束位置用TIdFTP什么属性来设置?

end;
try
//TIdFTP1.Get(temFileName,tStream,true);
//开始下载
TIdFTP1.Get(tURL,tStream);
//开始下载
Form1.ListBox1.ItemIndex := Form1.ListBox1.Items.Add(temFileName+'download');
finally
tStream.Free;
end;
end;

procedure TThread1.Execute;
begin
if Form1.Edit4.Text<>'' then
synchronize(DownLodeFile)
else
exit;
end;
 
帮顶!
http://www.source520.com
站长开发推广同盟 站长朋友的终极驿站
同时拥有海量源码电子经典书籍下载
http://www.source520.com/search/search.asp
&quot;编程.站长&quot;论坛搜索引擎-----为中国站长注入动力!
 
unit Unit2;
interface
uses
Classes, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdExplicitTLSClientServerBase, IdFTP, DateUtils, sysutils,windows;
type
TSendFileThread = class(TThread)
private
{ Private declarations }
protected
filename: String;
procedure Execute;
override;
public
constructor Create(number:integer);
end;

implementation
uses Unit1;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TSendFileThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TSendFileThread }
constructor TSendFileThread.Create(number: integer);
begin
filename:=inttostr(number)+'.txt';
inherited Create(False);
end;

procedure TSendFileThread.Execute;
var
IdFtpTemp: TIdFtp;
sl: TStringList;
begin
{ Place thread code here }
// sl := TStringList.Create;
// sl.Add(DateTimeToStr(Now));
// sl.SaveToFile(filename);
// sl.Clear;
// Freeandnil(sl);
idftptemp := Tidftp.Create;
idftptemp.Username := 'anonymous';
idftptemp.Host := '127.0.0.1';
idftptemp.Connect;
idftptemp.Login;
idftptemp.Put('mirserver.rar',filename);
idftptemp.Disconnect;
idftptemp.Free;
end;

end.
 
怎么downloadfile方法可以不用参数的?
楼主能否解释一下
 

Similar threads

后退
顶部