HanDs
管理员

[Delphi文章] TCP/IP下载文件(分块下载,带进度条显示) 



////////////////////////////////////////////客户端////////////////////////////////////////////

 

//CurFilePath:本地路径

//SerFilePath:服务器路径

//CurFileName:本地文件名

//SerFileName:服务器文件名

//ShowFlag:是否显示进度条窗口

 

Function Act_DownFiles(CurFilePath,SerFilePath,CurFileName,SerFileName:String;ShowFlag:Boolean):Boolean;
var
  TemFileName,RecevFileName:String;
  rbyte:array[0..40960] of byte;
  sFile:TFileStream;
  iFileSize,TemInt:integer;
begin

  Result:=False;

  TemInt:=40960;

  IdTCPClientDowFiles.Host :=serverip;//服务器的地址

  if IdTCPClientDowFiles.Connected then
    IdTCPClientDowFiles.Disconnect;

  Try
    IdTCPClientDowFiles.Connect;
  except
    MessageBox(Handle,'服务器没有开启','提示',MB_OK);
    Result:=False;
    Exit;
  end;

 

  with   IdTCPClientDowFiles   do
  begin
    while   Connected   do
    begin
      Try
        TemFileName:=SerFilePath+SerFileName;//服务器路径加名称
        WriteLn(TemFileName); //指定路径

        RecevFileName:=ReadLn;//从服务器端获得文件名

        if RecevFileName <> '文件不存在' then
        begin
          iFileSize:=IdTCPClientDowFiles.ReadInteger;
          sFile:=TFileStream.Create(CurFilePath+CurFileName,fmCreate);

          if ShowFlag then
          begin
            FrmProgress.Show;
            FrmProgress.PB1.Position:=0;
            FrmProgress.PB1.Max :=  iFileSize div 100 ;
          end;

          While iFileSize > TemIntdo
          begin
            IdTCPClientDowFiles.ReadBuffer(rbyte,TemInt);// .ReadBuffer(rbyte,iLen);
            sFile.Write(rByte,TemInt);
            inc(iFileSize,-TemInt);

            Application.ProcessMessages;
            if ShowFlag then
            begin
              FrmProgress.Label1.Caption:='正在下载:'+SerFileName;
              FrmProgress.PB1.Position:= FrmProgress.PB1.Position +(TemIntdiv 100) ;
            end;
          end;

 

          if iFileSize > 0 then
            IdTCPClientDowFiles.ReadBuffer(rbyte,iFileSize);// .ReadBuffer(rbyte,iLen);

 

          Application.ProcessMessages;

          sFile.Write(rByte,iFileSize);

 

          if ShowFlag then
          begin
            FrmProgress.Close;
          end;
         
          sFile.Free;
        end;
      finally
        Disconnect;//断开连接
      end;
    end;
  end;
  Result:=True;
end;

 

////////////////////////////////////////////服务端////////////////////////////////////////

 

IdTCPServerGetFiles.Active:=True;

 

procedure IdTCPServerGetFilesExecute(AThread: TIdPeerThread);
var
  RecevFileName:string;
  iFileLen,cnt:integer;
  buf:array[0..40960] of byte;
  TemFile:file of byte;
begin
  if not AThread.Terminated and AThread.Connection.Connected then  //注意这里
  begin
    with   AThread.Connection   do
    begin
      Try
        RecevFileName:=AThread.Connection.ReadLn;

        if FileExists(RecevFileName) then
        begin
          Try
            WriteLn(RecevFileName);

            Assignfile(TemFile,RecevFileName);
            Reset(TemFile);//打开文件
            iFileLen:=FileSize(TemFile);

            AThread.Connection.WriteInteger(iFileLen,True);////hjh 20071009

            while iFileLen > 40960 do
            begin
              cnt:=40960;
              BlockRead(TemFile,buf[0],cnt); //分块读取
              AThread.Connection.WriteBuffer(buf,cnt,True);/////hjh20071009
              iFileLen:=iFileLen-cnt;
            end;

            if iFileLen > 0 then
            begin
              cnt:=iFileLen;
              BlockRead(TemFile,buf[0],cnt);
              AThread.Connection.WriteBuffer(buf,cnt,True);/////hjh20071009
            end;

          Finally
            CloseFile(TemFile);//关闭文件
          end;
        end
        else
        begin
          WriteLn('文件不存在');
        end;
      Finally
        Disconnect;//断开连接
      end;
    end;
  end;
end;


学习中请遵守法律法规,本网站内容均来自于互联网,本网站不负担法律责任
TCP/IP 下载文件 ( 分块下载 带进度条显示 )
#1楼
发帖时间:2016-7-9   |   查看数:0   |   回复数:0
游客组
快速回复