//连接FTP
var
i:integer;
tr : Tstrings;
IdFTP1.Quit;
tr := TStringlist.Create;
IdFTP1.Host := hostTxt.Text; //FTP服务器地址 172.20.16.76
IdFTP1.Port := StrToInt(PortTxt.Text); //FTP服务器端口号9500
IdFTP1.Username := UserTxt.Text; //FTP服务器用户名
IdFTP1.Password := pwdTxt.Text; //FTP服务器密码
IdFTP1.Connect(); //连接到ftp
IdFTP1.ChangeDir('JMPZ');
IdFTP1.List(tr); //得到目录下所有文件列表
FFTxt.Items.Assign(tr);
tr.Free;
//ftp当前路径,从登陆用户的起始目录起始
IdFTP1.RetrieveCurrentDir
//ftp目录变换,DirName只能是上一目录,或者下一目录,无需加\"/\"
IdFTP1.ChangeDir(DirName);
idftp_Client.Connected// 判断是否连接
//要使用IdFTP1.DirectoryListing属性,必须uses IdAllFTPListParsers;否则会有no idftplistparse classes have been registered的错误
//===========================================================
UseMLIS为目录解析器相关的参数,不同的Ftp服务器,会有不同的格式
//使用idftp1.list命令获取的目录列表时,显示列表为
drw-rw-rw- 1 user group 0 Jun 26 19:30 JMPZ
drw-rw-rw- 1 user group 0 Jun 26 19:31 加密配置
若进入二级目录,则多显示两行
drw-rw-rw- 1 user group 0 Jun 26 19:30 .
drw-rw-rw- 1 user group 0 Jun 26 19:30 ..
drw-rw-rw- 1 user group 0 Apr 5 09:42 0旧订单
drw-rw-rw- 1 user group 0 Apr 5 09:29 11rrrrrrrr
但是当参数IDFTP1.UseMLIS:= true;时,显示列表为
Type=dir;Modify=20170626113034;Win32.ea=0x00000010; JMPZ
Type=dir;Modify=20170626113101;Win32.ea=0x00000010; 加密配置
若进入二级目录,则多显示行
Type=cdir;Modify=20170626113034;Win32.ea=0x00000010; /JMPZ
Type=dir;Modify=20170405014212;Win32.ea=0x00000010; 0旧订单
Type=dir;Modify=20170405012904;Win32.ea=0x00000010; 11rrrrrrrr
Type=dir;Modify=20170608010420;Win32.ea=0x00000010; 4501036094
或者(不同的Ftp服务器,会有不同的格式)
11-16-16 04:59PM
06-28-17 09:06AM
11-15-16 08:43AM
09-30-15 08:43AM
09-22-15 03:52PM
//=================================================================
//上传文件,要保证FTP路径正确,文件夹存在,默认会覆盖
uses IdFTPCommon;//ftBinary
IdFTP1.Put('本地文件及路径', AnsiToUtf8('FTP文件夹相对路径'));
IdFTP1.TransferType := ftBinary;
IdFTP1.Put('1.txt', AnsiToUtf8('4501036094\\Main.txt'));
//================================================================
//创建文件夹,调试模式下,会报错
try
IdFTP1.ChangeDir('000');
except
IdFTP1.MakeDir('000');
IdFTP1.ChangeDir('000');
end;
//======================================================================
//判断文件是否存在
if idftp1.Size('ip.bin')=-1 then
begin
NoteFlag('下载失败',clred);Exit;
end;
//====================================================================
//下载文件夹内的所有文件及文件夹
procedure TForm1.DownloadDirectory(var idFTP: TIdFtp;LocalDir, RemoteDir: string);
var
i,DirCount: Integer;
strName: string;
begin
if Copy(LocalDir,Length(LocalDir),1)<>'\\' then
LocalDir:=LocalDir+'\\';
if not DirectoryExists(LocalDir + RemoteDir) then
begin
CreateDirectory(PChar(LocalDir + RemoteDir),nil); //创建一个全路径的文件夹
LogTxt.Lines.Add('建立目录:' + LocalDir + RemoteDir);
end;
idFTP.ChangeDir(AnsiToUtf8(RemoteDir));
idFTP.TransferType := ftASCII;
idFTP.List(nil);
DirCount := idFTP.DirectoryListing.Count;
for i := 0 to DirCount - 1 do
begin
strName := Utf8ToAnsi(idFTP.DirectoryListing.Items[i].FileName);
LogTxt.Lines.Add('解析文件:' + strName);
if idFTP.DirectoryListing.Items[i].ItemType = ditDirectory then
if (strName = '.') or (strName = '..') then
Continue
else
begin
DownloadDirectory(idFTP,LocalDir + RemoteDir + '\\', strName);
idFTP.ChangeDir('..');
idFTP.List(nil);
end
else
begin
if (ExtractFileExt(strName) = '.txt') or (ExtractFileExt(strName) = '.html') or (ExtractFileExt(strName) = '.htm') then
idFTP.TransferType := ftASCII //文本模式
else
idFTP.TransferType := ftBinary; //二进制模式
idFTP.Get(AnsiToUtf8(strName), LocalDir + RemoteDir + '\\' + strName, True);
LogTxt.Lines.Add('下载文件:' + strName);
end;
Application.ProcessMessages;
end;
end;
//调用
DownloadDirectory(idftp1,ExtractFilePath(Application.ExeName),'H_C');
//ExtractFilePath(Application.ExeName):下载到本地文件夹
//H_C:FTP的文件夹
因篇幅问题不能全部显示,请点此查看更多更全内容