FTP多线程切割传输

1.数据分割和合并,采用流文件
  1. unit UnitPhotoData;
  2. interface
  3. uses
  4.   Windows, SysUtils, Variants, Classes ,StdCtrls;
  5. type
  6.   TPhotoData = class(TObject)
  7.   private
  8.     { Private declarations }
  9.   public
  10.     procedure SplitData(aFileName:string;aDataSize:integer;
  11.                         aFileNameList:TStrings);
  12.     procedure CompoundData(aHeadFileName:string);
  13.     { Public declarations }
  14.   end;
  15. implementation
  16. { TPhotoData }
  17. procedure TPhotoData.SplitData(aFileName: string;aDataSize:integer;
  18.                                aFileNameList:TStrings);
  19. var
  20.   i, aRealSize : Integer;
  21.   InStream, OutStream : TFileStream; 
  22.   s : string;
  23. begin
  24.   i := 0;
  25.   InStream:=TFileStream.Create(aFileName,fmOpenRead);
  26.   try
  27.     while (InStream.Position < InStream.Size) do begin
  28.       s := IntToStr(i);
  29.       while Length(s) < 3 do s := '0' + s;
  30.       s := '.' + s;
  31.       aFileNameList.Add(aFileName+s);
  32.       if InStream.Size - InStream.Position < aDataSize then
  33.          aRealSize := InStream.Size - InStream.Position
  34.       else
  35.          aRealSize := aDataSize;
  36.          OutStream := TFileStream.Create(aFileName + s,fmCreate);
  37.         try
  38.          OutStream.CopyFrom(InStream,aRealSize);
  39.          Inc(i);
  40.         finally 
  41.          OutStream.Free;
  42.         end;
  43.     end;
  44.    finally
  45.      InStream.Free;
  46.    end;
  47. end;
  48. procedure TPhotoData.CompoundData(aHeadFileName:string);
  49. var
  50.   i : Integer;
  51.   InStream, OutStream : TFileStream;
  52.   ExtName, s : string;
  53. begin
  54.   i := 0;
  55.   s := IntToStr(I);
  56.   while Length(s) < 3 do s := '0' + s ;
  57.     s := '.'+s;
  58.     ExtName := ChangeFileExt(ExtName, s);
  59.     if FileExists(aHeadFileName+ExtName) then begin
  60.       OutStream := TFileStream.Create(aHeadFileName,fmCreate);
  61.       try
  62.         while FileExists(aHeadFileName+ExtName) do begin
  63.           InStream := TFileStream.Create(aHeadFileName + ExtName,fmOpenRead);
  64.           try 
  65.             OutStream.CopyFrom(InStream,0);
  66.             Inc(i);
  67.             s := IntToStr(I);
  68.             while Length(s) < 3 do s := '0' + s;
  69.             s := '.'+s;
  70.             ExtName := ChangeFileExt(ExtName, s);
  71.           finally
  72.             InStream.Free;
  73.           end;
  74.         end;
  75.      finally
  76.       OutStream.Free;
  77.      end;
  78.   end;
  79. end;
  80. end.
2.FTP线程类
  1. unit UnitFtpPhotoData;
  2. interface
  3. uses
  4.   Windows, SysUtils, Variants, Classes ,StdCtrls,Controls,IdBaseComponent,
  5.   IdComponent,IdTCPConnection, IdTCPClient, IdFTP,
  6.   IdThreadComponent, IdAntiFreezeBase, IdAntiFreeze,Dialogs;
  7. const
  8.    ZZ_FTP_IP = '127.0.0.1' ;
  9.    ZZ_FTP_PORT = 21 ;
  10. type
  11.   TFtpPhotoData = class(TIdFtp)
  12.   private
  13.     { Private declarations }
  14.   public
  15.     procedure CreateFtpThread(idFTP:TIdFTP;aUserName,aPassword,
  16.                               aOrgFileName,aDestFileName:string) ;
  17.     procedure DoWork(AWorkMode: TWorkMode; const ACount: Integer); override;
  18.     procedure EndWork(AWorkMode: TWorkMode); override;
  19.    published
  20.     { Published declarations }
  21.     {公布子组件的事件}
  22.     { Public declarations }
  23.   end;
  24. type
  25.   TFtpThread = class(TIdThreadComponent)
  26.   private
  27.   public
  28. end ;
  29. implementation
  30. { TFtpPhotoData }
  31. procedure TFtpPhotoData.CreateFtpThread(idFTP:TIdFTP;aUserName,aPassword,
  32.                                        aOrgFileName,aDestFileName:string);
  33. begin
  34.   idFtp.Host := ZZ_FTP_IP ;
  35.   idFtp.Port := ZZ_FTP_PORT ;
  36.   idFtp.Username := aUserName ;
  37.   idFtp.Password := aPassword ;
  38.   try
  39.     idFtp.Connect;
  40.      if not idFtp.Connected then begin
  41.         //MsgBox('');
  42.       end else begin
  43.          idFtp.Put(aOrgFileName,aDestFileName,false);
  44.          //idFtp.
  45.       end ;
  46.     except
  47.       idFtp.Quit;
  48.     end ;
  49. end;
  50. procedure TFtpPhotoData.DoWork(AWorkMode: TWorkMode; const ACount: Integer);
  51. begin
  52.   if ACount = 100 then begin
  53.       showMessage('finish Thread');
  54.   end ;
  55. end ;
  56. procedure TFtpPhotoData.EndWork(AWorkMode: TWorkMode);
  57. begin
  58.   showMessage('finish Thread');
  59.   //
  60. end;
  61. end.
3.调用程序
  1. //TIdThreadComponent;TIdAntiFreeze;不常用的控件,线程和冻结
  2. procedure TFrm_Ftp.IdThreadComponent_FtpRun(Sender: TIdCustomThreadComponent);
  3. var
  4.   f: TIdFTP;
  5.   i : integer ;
  6. begin
  7.   f := TIdFTP.Create(Self);
  8.   try
  9.     f.Username := 'ftp';
  10.     f.Password := 'ftp';
  11.     f.Host := '127.0.0.1';
  12.     f.Port := 21;
  13.     if FThreadNumber > 3 then begin
  14.       self.IdThreadComponent_Ftp.Terminate;
  15.        self.IdThreadComponent_Ftp.Active := False ;
  16.         exit ;
  17.     end ;
  18.       try
  19.         f.Connect;
  20.          f.Put(FFileNameList[FThreadNumber],ExtractFileName(FFileNameList[FThreadNumber]),False);
  21.         sleep(10000);
  22.         f.Quit;
  23.         self.IdThreadComponent_Ftp.Terminate;
  24.       except
  25.       end;
  26.   finally
  27.     f.Free;
  28.   end;
  29.   inc(FThreadNumber);
  30. end;
放在BLOG上,以后有机会再用,免得忘了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值