1.数据分割和合并,采用流文件3.调用程序
- unit UnitPhotoData;
- interface
- uses
- Windows, SysUtils, Variants, Classes ,StdCtrls;
- type
- TPhotoData = class(TObject)
- private
- { Private declarations }
- public
- procedure SplitData(aFileName:string;aDataSize:integer;
- aFileNameList:TStrings);
- procedure CompoundData(aHeadFileName:string);
- { Public declarations }
- end;
- implementation
- { TPhotoData }
- procedure TPhotoData.SplitData(aFileName: string;aDataSize:integer;
- aFileNameList:TStrings);
- var
- i, aRealSize : Integer;
- InStream, OutStream : TFileStream;
- s : string;
- begin
- i := 0;
- InStream:=TFileStream.Create(aFileName,fmOpenRead);
- try
- while (InStream.Position < InStream.Size) do begin
- s := IntToStr(i);
- while Length(s) < 3 do s := '0' + s;
- s := '.' + s;
- aFileNameList.Add(aFileName+s);
- if InStream.Size - InStream.Position < aDataSize then
- aRealSize := InStream.Size - InStream.Position
- else
- aRealSize := aDataSize;
- OutStream := TFileStream.Create(aFileName + s,fmCreate);
- try
- OutStream.CopyFrom(InStream,aRealSize);
- Inc(i);
- finally
- OutStream.Free;
- end;
- end;
- finally
- InStream.Free;
- end;
- end;
- procedure TPhotoData.CompoundData(aHeadFileName:string);
- var
- i : Integer;
- InStream, OutStream : TFileStream;
- ExtName, s : string;
- begin
- i := 0;
- s := IntToStr(I);
- while Length(s) < 3 do s := '0' + s ;
- s := '.'+s;
- ExtName := ChangeFileExt(ExtName, s);
- if FileExists(aHeadFileName+ExtName) then begin
- OutStream := TFileStream.Create(aHeadFileName,fmCreate);
- try
- while FileExists(aHeadFileName+ExtName) do begin
- InStream := TFileStream.Create(aHeadFileName + ExtName,fmOpenRead);
- try
- OutStream.CopyFrom(InStream,0);
- Inc(i);
- s := IntToStr(I);
- while Length(s) < 3 do s := '0' + s;
- s := '.'+s;
- ExtName := ChangeFileExt(ExtName, s);
- finally
- InStream.Free;
- end;
- end;
- finally
- OutStream.Free;
- end;
- end;
- end;
- end.
2.FTP线程类
- unit UnitFtpPhotoData;
- interface
- uses
- Windows, SysUtils, Variants, Classes ,StdCtrls,Controls,IdBaseComponent,
- IdComponent,IdTCPConnection, IdTCPClient, IdFTP,
- IdThreadComponent, IdAntiFreezeBase, IdAntiFreeze,Dialogs;
- const
- ZZ_FTP_IP = '127.0.0.1' ;
- ZZ_FTP_PORT = 21 ;
- type
- TFtpPhotoData = class(TIdFtp)
- private
- { Private declarations }
- public
- procedure CreateFtpThread(idFTP:TIdFTP;aUserName,aPassword,
- aOrgFileName,aDestFileName:string) ;
- procedure DoWork(AWorkMode: TWorkMode; const ACount: Integer); override;
- procedure EndWork(AWorkMode: TWorkMode); override;
- published
- { Published declarations }
- {公布子组件的事件}
- { Public declarations }
- end;
- type
- TFtpThread = class(TIdThreadComponent)
- private
- public
- end ;
- implementation
- { TFtpPhotoData }
- procedure TFtpPhotoData.CreateFtpThread(idFTP:TIdFTP;aUserName,aPassword,
- aOrgFileName,aDestFileName:string);
- begin
- idFtp.Host := ZZ_FTP_IP ;
- idFtp.Port := ZZ_FTP_PORT ;
- idFtp.Username := aUserName ;
- idFtp.Password := aPassword ;
- try
- idFtp.Connect;
- if not idFtp.Connected then begin
- //MsgBox('');
- end else begin
- idFtp.Put(aOrgFileName,aDestFileName,false);
- //idFtp.
- end ;
- except
- idFtp.Quit;
- end ;
- end;
- procedure TFtpPhotoData.DoWork(AWorkMode: TWorkMode; const ACount: Integer);
- begin
- if ACount = 100 then begin
- showMessage('finish Thread');
- end ;
- end ;
- procedure TFtpPhotoData.EndWork(AWorkMode: TWorkMode);
- begin
- showMessage('finish Thread');
- //
- end;
- end.
- //TIdThreadComponent;TIdAntiFreeze;不常用的控件,线程和冻结
- procedure TFrm_Ftp.IdThreadComponent_FtpRun(Sender: TIdCustomThreadComponent);
- var
- f: TIdFTP;
- i : integer ;
- begin
- f := TIdFTP.Create(Self);
- try
- f.Username := 'ftp';
- f.Password := 'ftp';
- f.Host := '127.0.0.1';
- f.Port := 21;
- if FThreadNumber > 3 then begin
- self.IdThreadComponent_Ftp.Terminate;
- self.IdThreadComponent_Ftp.Active := False ;
- exit ;
- end ;
- try
- f.Connect;
- f.Put(FFileNameList[FThreadNumber],ExtractFileName(FFileNameList[FThreadNumber]),False);
- sleep(10000);
- f.Quit;
- self.IdThreadComponent_Ftp.Terminate;
- except
- end;
- finally
- f.Free;
- end;
- inc(FThreadNumber);
- end;
放在BLOG上,以后有机会再用,免得忘了。