以下是我写的基本可用但在某些情况下不可用. unit testftp;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP,iduri,StrUtils, StdCtrls;type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; IdFTP1: TIdFTP; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } function TestUrlFtp(url: string): boolean; end;var Form1: TForm1;implementation {$R *.dfm} function TForm1.TestUrlFtp(url: string): boolean; Var LS: TStringList;i:integer;idftptest:tidftp; URI:tiduri;fname,s:string; begin testurlFtp:=false; //默认返回测试不成功 url:=uri.URLDecode(url); //showmessage(url); URI := TIdURI.Create(url); //建立URL用于分析FTP的URL部分 //提取文件名部分 s:= URL; i := Pos('/', s); while i <> 0 do //去掉"/"前面的内容剩下的就是文件名了 begin Delete(s, 1, i); i := Pos('/', s); end; fname := s; //showmessage(fname); //提取文件名部分结束 if (length(fname)<1) or (uppercase(leftstr(url,6))<>'FTP://') then begin testUrlFtp:=false;exit;end; try TRY //idftp1:=tidftp.Create(nil); LS := TStringList.Create; idftp1.Host:=uri.Host; showmessage(uri.host); //idftp1.Port:=21; if uri.port<>'' then idftp1.Port:=strtoint(uri.Port); idftp1.Username:='ftp'; if uri.Username<>'' then idftp1.Username:=uri.Username; idftp1.Password:='ftp'; if uri.password<>'' then idftp1.password:=uri.password; SHOWMESSAGE(URI.Path); // setftpProxy(idftp1,proxyInfo); idftp1.Connect; showmessage(uri.path); idftp1.ChangeDir(uri.path); idftp1.list(ls,fname); if idftp1.DirectoryListing.Count>0 then testurlFtp:=true; for i:=0 to idftp1.DirectoryListing.Count-1 do if idftp1.DirectoryListing[i].FileName=fname then showmessage(idftp1.DirectoryListing[i].FileName); except testUrlFtp:=false; end; finally idftp1.Free; LS.Free; end; end; procedure TForm1.Button1Click(Sender: TObject); begin if testurlftp(edit1.text) then showmessage('成功') else showmessage('失败'); end;end.
最直接就是这几行: idftp1.Connect; showmessage(uri.path); idftp1.ChangeDir(uri.path); idftp1.list(ls,fname); if idftp1.DirectoryListing.Count>0 then testurlFtp:=true; 当然showmessage是 调试用的.
unit testftp;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdFTP,iduri,StrUtils, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
IdFTP1: TIdFTP;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function TestUrlFtp(url: string): boolean;
end;var
Form1: TForm1;implementation
{$R *.dfm}
function TForm1.TestUrlFtp(url: string): boolean;
Var LS: TStringList;i:integer;idftptest:tidftp;
URI:tiduri;fname,s:string;
begin
testurlFtp:=false; //默认返回测试不成功
url:=uri.URLDecode(url);
//showmessage(url);
URI := TIdURI.Create(url); //建立URL用于分析FTP的URL部分
//提取文件名部分
s:= URL;
i := Pos('/', s);
while i <> 0 do //去掉"/"前面的内容剩下的就是文件名了
begin
Delete(s, 1, i);
i := Pos('/', s);
end;
fname := s;
//showmessage(fname);
//提取文件名部分结束
if (length(fname)<1) or (uppercase(leftstr(url,6))<>'FTP://') then
begin testUrlFtp:=false;exit;end; try
TRY
//idftp1:=tidftp.Create(nil);
LS := TStringList.Create;
idftp1.Host:=uri.Host;
showmessage(uri.host);
//idftp1.Port:=21;
if uri.port<>'' then
idftp1.Port:=strtoint(uri.Port);
idftp1.Username:='ftp';
if uri.Username<>'' then
idftp1.Username:=uri.Username;
idftp1.Password:='ftp';
if uri.password<>'' then
idftp1.password:=uri.password;
SHOWMESSAGE(URI.Path);
// setftpProxy(idftp1,proxyInfo);
idftp1.Connect;
showmessage(uri.path);
idftp1.ChangeDir(uri.path);
idftp1.list(ls,fname);
if idftp1.DirectoryListing.Count>0 then
testurlFtp:=true;
for i:=0 to idftp1.DirectoryListing.Count-1 do
if idftp1.DirectoryListing[i].FileName=fname then
showmessage(idftp1.DirectoryListing[i].FileName);
except
testUrlFtp:=false;
end;
finally
idftp1.Free;
LS.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if testurlftp(edit1.text) then
showmessage('成功')
else
showmessage('失败');
end;end.
idftp1.Connect;
showmessage(uri.path);
idftp1.ChangeDir(uri.path);
idftp1.list(ls,fname);
if idftp1.DirectoryListing.Count>0 then
testurlFtp:=true;
当然showmessage是
调试用的.
自己看文档都知道啦Indy9是使用IOHandler来支持Socks代理的.Indy官方站点有Demo的http://lysoft.7u7.net