• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphi实现FTP下载

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
unit WLFtp;

interface

uses
           Windows, Messages, Variants,SysUtils, Classes, Wininet, Dialogs;

type
           TWLFtp = class(TObject)

           private
                       FInetHandle: HInternet; // 句柄
                       FFtpHandle: HInternet; // 句柄

                       FHost: string; // 主机IP地址
                       FUserName: string; // 用户名
                       FPassword: string; // 密码
                       FPort: integer; // 端口

                       FCurrentDir: string; // 当前目录

           public
                       constructor Create;virtual;
                       destructor Destroy;override;

                       function Connect: boolean;
                       function Disconnect: boolean;

                       function UploadFile(RemoteFile: PChar; NewFile: PChar): boolean;
                       function DownloadFile(RemoteFile: PChar; NewFile: PChar): boolean;

                       function CreateDirectory(Directory: PChar): boolean;

                       function LayerNumber(dir: string): integer;
                       function MakeDirectory(dir: string): boolean;
                       function FTPMakeDirectory(dir: string): boolean;
                       function IndexOfLayer(index: integer; dir: string): string;
                       function GetFileName(FileName: string): string;
                       function GetDirectory(dir: string): string;

                       property InetHandle: HInternet read FInetHandle write FInetHandle;
                       property FtpHandle: HInternet read FFtpHandle write FFtpHandle;
                       property Host: string read FHost write FHost;
                       property UserName: string read FUserName write FUserName;
                       property Password: string read FPassword write FPassword;
                       property Port: integer read FPort write FPort;

                       property CurrentDir: string read FCurrentDir write FCurrentDir;

end;


implementation

//-------------------------------------------------------------------------
// 构造函数
constructor TWLFtp.Create;
begin
           inherited Create;

end;

//-------------------------------------------------------------------------
// 析构函数
destructor TWLFtp.Destroy;
begin

           inherited Destroy;
end;

//-------------------------------------------------------------------------
// 链接服务器
function TWLFtp.Connect: boolean;
begin
           try
                       Result := false;
                       // 创建句柄
                       FInetHandle := InternetOpen(PChar('KOLFTP'), 0, nil, nil, 0);
                       FtpHandle := InternetConnect(FInetHandle, PChar(Host), FPort, PChar(FUserName),
                                                                       PChar(FPassword), INTERNET_SERVICE_FTP, 0, 255);
                       if Assigned(FtpHandle) then
                       begin
                                   Result := true;
                       end;

           except
                       Result := false;
           end;
end;

//-------------------------------------------------------------------------
// 断开链接
function TWLFtp.Disconnect: boolean;
begin
           try
                       InternetCloseHandle(FFtpHandle);
                       InternetCloseHandle(FInetHandle);
                       FtpHandle:=nil;
                       inetHandle:=nil;

                       Result := true;
           except
                       Result := false;
           end;
end;

//-------------------------------------------------------------------------
// 上传文件
function TWLFtp.UploadFile(RemoteFile: PChar; NewFile: PChar): boolean;
begin
           try
                       Result := true;
                       FTPMakeDirectory(NewFile);
                       if not FtpPutFile(FFtpHandle, RemoteFile, NewFile,
                                                           FTP_TRANSFER_TYPE_BINARY, 255) then
                       begin
                                   Result := false;
                       end;
           except
                       Result := false;
           end;
end;

//-------------------------------------------------------------------------
// 下载文件
function TWLFtp.DownloadFile(RemoteFile: PChar; NewFile: PChar): boolean;
begin
           try
                       Result := true;
                       MakeDirectory(NewFile);
                       if not FtpGetFile(FFtpHandle, RemoteFile, NewFile,
                                                                                   True, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY OR INTERNET_FLAG_RELOAD, 255) then
                       begin
                                   Result := false;
                       end;
           except
                       Result := false;
           end;
end;

//-------------------------------------------------------------------------
// 创建目录
function TWLFtp.CreateDirectory(Directory: PChar): boolean;
begin
           try
                       Result := true;
                       if FtpCreateDirectory(FFtpHandle, Directory)=false then
                       begin
                                   Result := false;
                       end;
           except
                       Result := false;
           end;
end;

//-------------------------------------------------------------------------
// 目录数
function TWLFtp.LayerNumber(dir: string): integer;
var
           i: integer;
           flag: string;
begin
           Result := 0;

           for i:=1 to Length(dir) do
           begin
                       flag := Copy(dir,i,1);
                       if (flag='\') or (flag='/') then
                       begin
                                   Result := Result + 1;
                       end;
           end;
end;

//-------------------------------------------------------------------------
// 创建目录
function TWLFtp.FTPMakeDirectory(dir: string): boolean;
var
           count, i: integer;
           SubPath: string;
begin
           Result := true;
           count := LayerNumber(dir);

           for i:=1 to count do
           begin
                       SubPath := IndexOfLayer(i, dir);
                       if CreateDirectory(PChar(CurrentDir+SubPath))=false then
                       begin
                                   Result := false;
                       end;
           end;
end;

//-------------------------------------------------------------------------
// 创建目录
function TWLFtp.MakeDirectory(dir: string): boolean;
var
           count, i: integer;
           SubPath: string;
           str: string;
begin
           Result := true;
           count := LayerNumber(dir);
           str := GetDirectory(dir);

           for i:=2 to count do
           begin
                       SubPath := IndexOfLayer(i, str);
                       if not DirectoryExists(SubPath) then
                       begin
                                   if not CreateDir(SubPath) then
                                   begin
                                               Result := false;
                                   end;
                       end;
           end;
end;

//-------------------------------------------------------------------------
// 获取index层的目录
function TWLFtp.IndexOfLayer(index: integer; dir: string): string;
var
           count, i: integer;
           ch: string;
begin
           Result := '';
           count := 0;
           for i:=1 to Length(dir) do
           begin
                       ch := Copy(dir, i, 1);
                       if (ch='\') or (ch='/') then
                       begin
                                   count := count+1;
                       end;
                       if count=index then
                       begin
                                   break;
                       end;
                       Result := Result + ch;
           end;
end;

//-------------------------------------------------------------------------
// 获取文件名
function TWLFtp.GetFileName(FileName: string): string;
begin
           Result := '';
           while (Copy(FileName, Length(FileName), 1)<>'\') and (Length(FileName)>0) do
           begin
                       Result := Copy(FileName, Length(FileName), 1)+Result;
                       Delete(FileName, Length(FileName), 1);
           end;
end;

//-------------------------------------------------------------------------
// 获取目录
function TWLFtp.GetDirectory(dir: string): string;
begin
           Result := dir;
           while (Copy(Result, Length(Result), 1)<>'\') and (Length(Result)>0) do
           begin
                       Delete(Result, Length(Result), 1);
           end;

{            if Copy(Result, Length), 1)='\' then
           begin
                       Delete(Result, 1, 1);
           end;}
end;

//-------------------------------------------------------------------------
end.

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap