unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IniFiles, StdCtrls, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdFTP, ComCtrls,md5;
type
TForm1 = class(TForm)
IdFTP1: TIdFTP;
Button1: TButton;
Button2: TButton;
ProgressBar1: TProgressBar;
m_Log: TMemo;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
{===========================================================
FTP 文件下载程序 2002-07-03 Hanfi
功能:下在指定目录下的所有文件(不包含子目录)
输入:Telecom.ini 文件的[FTP]段
输出:下载文件保存至当前目录下
设置Telecom.ini文件的[FTP]段
[ftp]
system=UNIX //设置目标服务器类型 UNIX,DOS
IP=127.0.0.1 //设置目标服务器的IP地址
USER=guest //设置下载用户名
PASSWORD=guest //设置下载用户密码(应将此部分加密)
SERVERPATH=/app/download //设置下载目录
===========================================================}
implementation
{$R *.dfm}
var
// 服务器信息变量
S_System,S_Host,S_Pass,S_User,S_Dir: String;
S_Files:TStrings;
procedure TForm1.FormCreate(Sender: TObject);
var
ServerIni: TIniFile;
begin
Top:=(Screen.Height-Height) div 2;
Left:=(screen.Width-width) div 2;
ServerIni := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'telecom.ini');
S_System:= ServerIni.ReadString('Ftp', 'SYSTEM', 'UNIX');
S_Host:= ServerIni.ReadString('Ftp', 'IP', 'IP');
S_User:=ServerIni.ReadString('Ftp', 'USER', 'USER');
S_Pass:=ServerIni.ReadString('Ftp', 'PASSWORD', 'Password');
S_Dir:=ServerIni.ReadString('Ftp', 'SERVERPATH', '/app/download');
ServerIni.Free;
with IdFTP1 do try
Button1.Enabled := False;
User := S_User;
Password := S_Pass;
Host := S_Host;
finally
Button1.Enabled := true;
end;
end;
function GetNameFromDirLine(Line: String; Var IsDirectory: Boolean): String;
Var
//在字符串中取得文件名
i: Integer;
DosListing: Boolean;
begin
IsDirectory := Line[1] = 'd';
DosListing := false;
for i := 0 to 7 do begin
if (i = 2) and not IsDirectory then begin
IsDirectory := Copy(Line, 1, Pos(' ', Line) - 1) = '<DIR>';
if not IsDirectory then
DosListing := Line[1] in ['0'..'9']
else DosListing := true;
end;
Delete(Line, 1, Pos(' ', Line));
While Line[1] = ' ' do Delete(Line, 1, 1);
if DosListing and (i = 2) then break;
end;
Result := Line;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
name,Line: String;
i:integer;
IsDirectory: Boolean;
i_Start:integer;
begin
Button1.Enabled:=False;
Button2.Enabled:=False;
m_Log.Lines.Clear;
if not IdFTP1.Connected then
begin
m_Log.Lines.Add('## 开始下载任务');
m_Log.Lines.Add('=========================================');
try
IdFTP1.Connect;
IdFTP1.ChangeDir(S_Dir);
IdFTP1.TransferType := ftASCII;
S_Files:=TStringList.Create;
IdFTP1.List(S_Files);
m_Log.Lines.Add('');
m_Log.Lines.Add('## '+datetimetostr(Now)+' 连接服务器'+S_Host+'成功!');
m_Log.Lines.Add('');
except
Button1.Enabled:=True;
Button2.Enabled:=True;
Button2.SetFocus;
m_Log.Lines.Add('');
m_Log.Lines.Add('## '+datetimetostr(Now)+' 连接服务器失败!');
m_Log.Lines.Add('');
m_Log.Lines.Add('=========================================');
m_Log.Lines.Add('## 数据下载失败');
m_Log.Lines.SaveToFile('log_'+datetimetostr(Date())+'.log');
m_Log.Lines.Add('## 数据下载完毕,生成日志文件:log_'+datetimetostr(Date())+'.log');
exit;
end;
end;
ProgressBar1.Position:=0;
ProgressBar1.Max:=S_Files.Count;
ProgressBar1.Min:=0;
ProgressBar1.Step:=1;
if S_System='UNIX' then
i_Start:=1
else
i_Start:=0;
for i:=i_Start to S_Files.Count-1 do
begin
Line := S_Files[i];
Name:=GetNameFromDirLine(Line, IsDirectory);
if not IsDirectory then
begin
try
IdFTP1.TransferType := ftBinary;
IdFTP1.Get(Name, Name, true);
ProgressBar1.StepIt;
m_Log.Lines.Add('文件:'+Name+' 下载完成;');
except
m_Log.Lines.Add('下载:'+Name+' 失败,无法覆盖本地 '+Name+' 文件;');
end;
end;
end;
try
ProgressBar1.Position:=ProgressBar1.Max;
Button1.Enabled:=True;
Button2.Enabled:=True;
Button2.SetFocus;
m_Log.Lines.Add('=========================================');
IdFTP1.Disconnect ;
m_Log.Lines.Add('## '+datetimetostr(Now)+' 断开服务器连接!');
m_Log.Lines.SaveToFile('log_'+datetimetostr(Date())+'.log');
m_Log.Lines.Add('## 数据下载完毕,生成日志文件:log_'+datetimetostr(Date())+'.log');
except
m_Log.Lines.SaveToFile('Err.log');
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
S_Files.Free;
end;
end.[/code]
附录: telecom.ini
[ftp]
SYSTEM=UNIX
IP=127.0.0.1
USER=guest
PASSWORD=guest
SERVERPATH=/app/download
使用 D6 中的 TIdFTP 控件,DEMO中有完整的例子
|
请发表评论