我在连接 FTP 服务器时遇到问题。大多数时候我像这样连接到 FTP 服务器:
// ftp is TidFTP
ftp.Host := '119.xxx.xxx.133';
ftp.Username := 'fnc';
ftp.Password := 'fnc';
ftp.Port := 21;
ftp.ConnectTimeout := 5000;
ftp.Connect;
我们的管理员给了我一个这样的链接
ftp://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a2c24290a7b7b736432323264323232647b7979" rel="noreferrer noopener nofollow">[email protected]</a>/Files/
访问 FTP。用户名是fnc,端口是21,密码是fnc。
如果我通过 Windows 资源管理器访问 FTP,我不会收到任何错误,我可以完美地放置文件。但如果我在代码中执行此操作,每当我尝试放置文件时,我都会收到非法端口命令错误。
请注意,我可以使用上面的代码连接到 ftp 服务器,但不能在其中放置任何文件。 提前致谢。
最佳答案
给定一个 FTP URL,您需要解析该 URL 并将其组件分配给各种 TIdFTP
属性和方法,例如:
var
URL: string;
Uri: TIdURI;
begin
...
URL := ...; // 'ftp://fnc:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89efe7eac9b8b8b0a7f1f1f1a7f1f1f1a7b8baba" rel="noreferrer noopener nofollow">[email protected]</a>/Files/'
Uri := TIdURI.Create(URL);
try
ftp.Host := Uri.Host;
if Uri.Port <> '' then
ftp.Port := StrToInt(Uri.Port)
else
ftp.Port := 21;
ftp.Username := Uri.Username;
ftp.Password := Uri.Password;
ftp.ConnectTimeout := 5000;
ftp.Connect;
if Uri.Path <> '/' then
ftp.ChangeDir(Uri.Path);
...
finally
Uri.Free;
end;
...
end;
关于delphi - 如何使用 ftp ://userName@host 连接到 FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32522609/