1,593
社区成员
发帖
与我相关
我的任务
分享
procedure TThreadFTP.Execute;
begin
inherited;
while (not StartAtOnce) and (not Terminated) do
Sleep(100);
Note:= TNote.Create;
Note.FilePath:= ExtractFilePath(Application.ExeName) + 'Note\' + IntToStr(Self.Handle) + '.txt';
try
try
if not Terminated then
DoExec;
except
end;
Note.AddLine('DoExec Finished!'+#13#10);
while not Terminated do
Sleep(100);
Note.AddLine('Thread Terminated!'+#13#10);
finally
Note.Free;
end;
end;
procedure TThreadFTP.DoExec;
var
FTP: TIdFTP;
RootDIR, RltDIR, File_Name: string;
I: Integer;
List: TStringList;
Stream: TStream;
begin
Stream:= TMemoryStream.Create;
List:= TStringList.Create;
FTP:= TIdFTP.Create(nil);
try
FTP.Host:= '192.168.0.2'; // FTP地址
FTP.Username:= '**';
FTP.Password:= '**';
FTP.Connect(True, 5000); // 连接
if FTP.Connected then begin
RootDIR:= Utf8ToAnsi( FTP.RetrieveCurrentDir ); // 获取根路径
RltDIR:= '/TextFile/11'; // 设置相对路径
FTP.ChangeDir( AnsiToUtf8( RootDIR+RltDIR ) ); // 切换当前目录
FTP.List(List); // 取当前目录的文件列表,不做该部貌似下面的DirectoryListing无法正常使用
List.Clear; // 清空列表
for I:= 0 to FTP.DirectoryListing.Count - 1 do begin
if Terminated then Exit;
with FTP.DirectoryListing.Items[I] do begin
if ItemType = ditFile then begin // 是文件
File_Name:= Utf8ToAnsi( FileName );
List.Add(File_Name); // 添加文件名到列表
end;
end;
end;
while not Terminated do begin
for I:= 0 to List.Count - 1 do begin
if Terminated then Exit;
TMemoryStream(Stream).Clear;
try
Note.AddLine('FTP.Get('+List.Strings[I]+')');
FTP.Get( AnsiToUtf8(RootDIR+RltDIR+'/'+List.Strings[I]), Stream); // 获取文件
Note.AddLine('...Ok'+LRLS);
except
on E: Exception do begin
Note.AddLine('...raise error'+#13#10);
Note.AddLine('errmsg: '+E.Message+#13#10);
raise;
end;
end;
TMemoryStream(Stream).Clear;
end;
end;
end;
finally
Note.AddLine('Free Objects');
Stream.Free;
List.Free;
FTP.Free;
Note.AddLine('...Ok'+#13#10);
end;
end;
while not Terminated do begin
for I:= 0 to List.Count - 1 do begin
if Terminated then Exit;
TMemoryStream(Stream).Clear;
try
Note.AddLine('FTP.Get('+List.Strings[I]+')');
FTP.Get( AnsiToUtf8(RootDIR+RltDIR+'/'+List.Strings[I]), Stream);
Note.AddLine('...Ok'+#13#10);
except
on E: Exception do begin
Note.AddLine('...raise error'+#13#10);
Note.AddLine('errmsg: '+E.Message+#13#10);
raise;
end;
end;
TMemoryStream(Stream).Clear;
end;
end;
end;
finally
Note.AddLine('Stream.Free;');
Stream.Free;
Note.AddLine('...Ok'+#13#10);
Note.AddLine('List.Free;');
List.Free;
Note.AddLine('...Ok'+#13#10);
Note.AddLine('FTP.Free;');
FTP.Free;
Note.AddLine('...Ok'+#13#10);
end;
FDataChannel := TIdSimpleServer.Create(nil); try
with TIdSimpleServer(FDataChannel) do begin
InitDataChannel;
BoundIP := (Self.IOHandler as TIdIOHandlerSocket).Binding.IP;
BoundPort := Self.DataPort;
BoundPortMin := Self.DataPortMin;
BoundPortMax := Self.DataPortMax;
BeginListen;
SendPort(Binding);
if AResume then begin
Self.SendCmd('REST ' + IntToStr(ADest.Position), [350]); {Do not translate}
end;
Self.SendCmd(ACommand, [125, 150, 154]); //APR: Ericsson Switch FTP
Listen;
ReadStream(ADest, -1, True); // 估计是这里的问题吧。
end;
finally
FreeAndNil(FDataChannel);
end;