procedure DownloadFile(strHost, strRemoteFileName, strLocalFileName: string; ClientSocket: TClientSocket);
var
  intReturnCode: Integer;
  s: string;
  szBuffer: array[0..128] of Char;
  FileOut: TFileStream;
begin
  if strRemoteFileName[1] <> '/' then
    strRemoteFileName := '/' + strRemoteFileName;
  FileOut := TFileStream.Create(strLocalFileName, fmCreate);
  try
    with ClientSocket do
    begin
      Host := strHost;
      ClientType := ctBlocking;
      Port := 80;
      try
        Open;
        s := 'GET ' + strRemoteFileName + '   HTTP/1.0'#13#10 +
             'Host: ' + strHost + #13#10#13#10;
        intReturnCode := Socket.SendBuf(Pointer(s)^, Length(s));
        if intReturnCode > 0 then
        begin
          while (intReturnCode > 0) do
          begin
            FillChar(szBuffer, SizeOf(szBuffer), 0);
            intReturnCode := Socket.ReceiveBuf(szBuffer, SizeOf(szBuffer));
            if intReturnCode > 0 then
              FileOut.Write(szBuffer, intReturnCode);
          end
        end
        else
          MessageDlg('No answer from server', mtError, [mbOk], 0);
        Close;
      except
        MessageDlg('No connection', mtError, [mbOk], 0);
      end;
    end;
  finally
    FileOut.Free
  end;
end;调用例子: procedure TForm1.Button1Click(Sender: TObject);
begin
  DownloadFile('www.scalabium.com', '/forums.htm', 'd:\forums.htm', ClientSocket1);
end;

解决方案 »

  1.   

    delphi中的nmftp控件中Download函数可以下载文件,我给你一个
    下载整个目录的例子:
    function tftp.ex_download(remote_dir,local_dir:string):boolean;
    var
        i,j,count1:integer;
        att,ss:string;
        current_dir:string;
        temp_dir:string;
    begin
        try begin
            NMFTP1.ChangeDir(remote_dir);
            current_dir:=remote_dir;
            temp_dir:=copy(current_dir,2,length(current_dir));
            if not DirectoryExists(local_dir) then CreateDir(local_dir);
            if not directoryexists(local_dir+temp_dir) then createdir(local_dir+temp_dir);
            nmftp1.ParseList:=true;
            NMftp1.list;
            count1:=nmftp1.FTPDirectoryList.name.Count;
            for i:=0 to count1-1  do begin
                file://必须
                NMFTP1.ChangeDir(current_dir);
                nmftp1.list;
                ss:=nmftp1.FTPDirectoryList.name.Strings[i];
                att:=nmftp1.FTPDirectoryList.Attribute.Strings[i];
                if (copy(pchar(att),1,1)<>'d')and(copy(pchar(att),1,1)<>'D') then begin
                    if not DirectoryExists(local_dir) then CreateDir(local_dir);
                    NMFTP1.Download(current_dir+ss,local_dir+temp_dir+ss);
                end
                else begin
                    if not directoryexists(local_dir+temp_dir+ss) then createdir(local_dir+temp_dir+ss);
                    file://递归调用
                    ex_download(remote_dir+ss+'\',local_dir);
                end;
            end;
               result:=true;
        end
        except
        On E:Exception do begin
            result:=false;
        end;
        end;
    end;
      

  2.   

    这些下载的例子对服务器有没有特别的要求呢?能不能吧下载的大概思想讲一下?还有怎么自动定位一个URL资源?