Delphi處理高速檔案上傳下載的代碼及思路
上傳和下載是一對方向不同的概念,下面對應的客戶端和服務器代碼:掉個頭,它就是下載;再掉個頭,它就是上傳,
一、思路
1、將大檔案:分段(即常說的“斷點”上傳或下載)上傳或下載
2、分別上傳或下載這些分段
3、將上傳或下載后的各“分段”檔案流合并還原
4、關于加速(網上你經常看到“高速”上傳或下載):若多執行緒分別上傳或下載這些分段,即可提速,
你們去使用 高勇老師的代碼,它是完全無問題;這里僅僅給大家提供一種提速的方案,高勇老師的代碼,你按照這個思路稍作改裝,即可高速上傳或下載,
二、代碼
2.1、客戶端:
///<summary>RestFull上傳檔案函式(檔案流方法):
///上傳單個檔案(TFileStream實體化抽象流:檔案分段傳參給服務器):
///</summary>
function CopyFileTFileStream(const Path: string;
const BufSize :Integer= (13*1024*1024); //Buffer大小默認值13MB
const ServerPath:string='api\upload\';
const ServerFileName:string='A1.pdf' ):string;
var Buffer: TBytes; ReadCount,FileSize,FileBufferCount,iCircle: Integer;
ATStream,AWriteStream: TFileStream; ACopyStream:TStream;
AResultStr:string; ifResult: Boolean;
begin
if (Path.Trim='')
or (ServerPath.Trim='')
or (ServerFileName.Trim='') then
begin
AResultStr:='檔案不能為空!';
Result := AResultStr ;
exit;
end;
if FileExists(Path) =true then
begin
AResultStr:='檔案存在!';
//AWriteStream:讀本地大檔案的流:
AWriteStream := TFileStream.Create( Path,fmOpenRead );
iCircle:=ceil( AWriteStream.Size /BufSize );
//:分幾段讀取,就寫幾個臨時檔案.tmp:
//大檔案:已知大于最小設定值,則分段讀取,直到讀完為止:
if AWriteStream.Size > BufSize then
//:不能用FileSize顯式的判斷
begin //:需要分資料包處理,如果是服務端是Datasnap默認32k的緩沖區(快取,太JB小啦):
//檔案流無需SetSize,而是設定其Buffer:TBytes位元組陣列的長度://SetLength(Buffer, BufSize);
SetLength( Buffer,(500*1024*1024) );
//$F000;//$F000:61440:60kb//:System.Classes.fmCreate
//:印象中好像最大緩沖區就這個大小,你自己核實吧,500兆我夠了
FileBufferCount:=0; FileSize:=BufSize;
AResultStr:='';
for FileBufferCount := 1 to iCircle do
begin //檔案流無需SetSize:
//ATStream:將大檔案流AWriteStream拆分成:
//三位數的小臨時檔案.tmp的檔案流:
ACopyStream:= TMemoryStream.Create;
{ATStream:=TFileStream.Create( Path
+ FillBeforeString(IntToStr(FileBufferCount),3,'0')+'.tmp',
fmCreate );
try
finally
ATStream.disposeOf;
end;//}
ATStream:=TFileStream.Create( Path
+FillBeforeString(IntToStr(FileBufferCount),3,'0')+'.tmp'
,fmCreate or fmOpenReadWrite);//:FillBeforeString:自寫函式,字串末尾向左補齊字符
//:檔案流,沒有就產生、有就打開
try
if FileSize < AWriteStream.Size then FileSize:= BufSize;
if FileSize >= AWriteStream.Size then
FileSize:=AWriteStream.Size - (iCircle-1) * BufSize;
ReadCount := AWriteStream.Read( Buffer[0], FileSize );
AResultStr:=AResultStr+','+IntToStr(FileSize);
if ReadCount > 0 then
ATStream.WriteBuffer(Buffer[0], ReadCount);
ACopyStream.CopyFrom(ATStream,0);
//:ACopyStream:由CopyFrom的呼叫者自己來釋放
try
ifResult:=ServerMethods
.upLoadFileFromTStreamBufferRead(ACopyStream,( ServerPath +ServerFileName
+FillBeforeString(IntToStr(FileBufferCount),3,'0')+'.tmp') );
//:服務器方法成功執行需要時間:
//while ifResult<>true do sleep(0);//:需要的場景才這樣做
except
AWriteStream.disposeOf; ATStream.disposeOf; ACopyStream.disposeOf;
Result := '流加載錯誤'; exit;
end;
FileSize:=FileSize + BufSize;
finally
//傳完該臨時分段檔案流釋放它的句柄,并洗掉它對應的臨時檔案:
ATStream.disposeOf;
System.IOUtils.TFile.Delete(Path
+FillBeforeString(IntToStr(FileBufferCount),3,'0')+'.tmp');
end;
end;
//確保:全部臨時分段檔案流處理完成之后,發出合并檔案請求,
//將分段檔案流還原為原始檔案:
if ifResult=true then
begin
try
ifResult:=ServerMethods
.mergeFilesUseTFileStream(ServerPath + ServerFileName);
//while ifResult <>true do sleep(0);//:服務器方法成功執行需要時間,需要的場景才做
except
AWriteStream.disposeOf; ATStream.disposeOf; ACopyStream.disposeOf;
Result := '檔案流合并錯誤'; exit;
end;
AResultStr:= '檔案上傳成功';
AWriteStream.disposeOf;
end;
end else
begin
//小檔案:已知小于最小設定值,則直接復制資料:
ACopyStream:= TMemoryStream.Create;
ACopyStream.CopyFrom(AWriteStream,0);
try
ifResult:=ServerMethods
.upLoadFileFromTStreamBufferRead( ACopyStream,(ServerPath+ServerFileName) );
//:服務器方法成功執行需要時間
//while ifResult<>true do sleep(0);//:需要的場景才這樣做
AResultStr:=IntToStr(AWriteStream.Size);
except
AWriteStream.disposeOf; ACopyStream.disposeOf;
Result := '服務器出錯了'; exit;
end;
if ifResult=true then
begin
AResultStr:= '檔案上傳成功';
AWriteStream.disposeOf;
end;
end;
end else AResultStr:='檔案不存在!';
System.IOUtils.TDirectory.SetCurrentDirectory
(System.IOUtils.TPath.GetLibraryPath);
Result := AResultStr ;
end;
2.2、服務端上傳的接收流、合并檔案流:
function mergeFilesUseTFileStream(const toFilePath:string): Boolean;
var filesCount:Integer; SearchRec: TSearchRec; currPath:string;
fileName:string; filesName:TStringList;
FileTStream,tmpTStream:TStream;
begin
TDirectory.SetCurrentDirectory(System.IOUtils.TPath.GetLibraryPath);
fileName:=System.IOUtils.TPath.GetLibraryPath + toFilePath;
if TDirectory.Exists(TDirectory.GetParent(fileName)) then
begin
currPath:=TDirectory.GetParent(fileName);
TDirectory.SetCurrentDirectory(currPath);
//:進入該路徑(設定當前路徑并進入)
if FileExists(fileName)=true then TFile.Delete(fileName);
end;
filesCount := FindFirst(currPath + '\' + '*.tmp', faAnyFile, SearchRec);//:搜索成功回傳0
if filesCount<>0 then
begin
System.SysUtils.FindClose(SearchRec);//?不識別:需要帶上前綴:System.SysUtils.
TDirectory.SetCurrentDirectory(System.IOUtils.TPath.GetLibraryPath);
Result :=false; exit;
end;
FileTStream:=TFileStream.Create(fileName,fmCreate);
filesName:=TStringList.Create;
while filesCount = 0 do
begin
filesName.Add(SearchRec.Name);
tmpTStream:=TFileStream.Create(SearchRec.Name,fmOpenRead);
try
Vcl.Forms.Application.ProcessMessages;
//:不卡界面:將執行緒池TServerMethods1的訊息事件交給應用的主執行緒
//:本函式:如果用于客戶端,下面兩句的處理,請使用執行緒同步TThread.Synchronize(服務端也可這樣),因為硬碟IO需要確切的時間來處理完畢:
FileTStream.CopyFrom(tmpTStream,0);
filesCount := FindNext(SearchRec);
finally
tmpTStream.Free;
end;
end;
try
for filesCount := 0 to filesName.Count-1 do
begin
if FileExists(filesName[filesCount].Trim)=true then System.IOUtils.TFile.Delete(filesName[filesCount].Trim);
end;
finally
System.SysUtils.FindClose(SearchRec);//?不識別:需要帶上前綴:System.SysUtils.
filesName.Free; FileTStream.Free;
TDirectory.SetCurrentDirectory(System.IOUtils.TPath.GetLibraryPath);
end;
//幾個常用的路徑方法:
//強制路徑產生://ForceDirectories(currPath) ;uses System.SysUtils;
//DirectoryExists:路徑是否存在uses System.SysUtils;
//FileExists:檔案是否存在:uses System.SysUtils;
//設定當前(進入該)路徑:TDirectory.SetCurrentDirectory(Path);
//路徑中是否什么檔案都沒有:TDirectory.IsEmpty(Path)
//是同級的路徑:TDirectory.IsRelativePath(Path)
//移動檔案夾:TDirectory.Move(const SourceDirName, DestDirName: string)
Result :=true;
end;
2.2、服務端:
/// <summary>大檔案上傳到服務器的函式;
///引數為檔案資料流和服務器端應用庫路徑下的子路徑 </summary>
function TServerMethods1.upLoadFileFromTStreamBufferRead(
const ATStream:TStream; const toFilePath:string): Boolean;
const BufSize = $F000; //:BufferSize //System.Classes.fmCreate
var Buffer: TBytes; ReadCount,times: Integer; FS: TFileStream;
begin
if FileExists(toFilePath)=true then
begin
TThread.Synchronize(nil,
procedure
begin
TFile.Delete(toFilePath);
MainServerForm.Memo_Errors.Lines.Add('洗掉檔案:'+toFilePath.Trim);
end
);
end;//:存在就先洗掉
//洗掉后然后產生新檔案:
FS := TFileStream.Create(toFilePath, System.Classes.fmCreate);
//:常量默認fmCreate = $FF00; //: TFileStream create mode
TThread.Synchronize(nil,
procedure
begin//測驗:
//MainServerForm.Memo_Errors.Lines.Add('新產生檔案:'+toFilePath.Trim);
end
);
try
//if ATStream.Size = -1 then//:大小未知則一直讀取到沒有資料為止:
begin
SetLength( Buffer, BufSize );
repeat
ReadCount := ATStream.Read( Buffer[0], BufSize );
if ReadCount > 0 then
FS.WriteBuffer( Buffer[0], ReadCount );
if ReadCount < BufSize then
break;
until ReadCount < BufSize;
end //else FS.CopyFrom(ATStream, 0)
; //:大小已知則直接復制資料
finally
FS.Free;
Result := True;
end;
end;
/// <summary>多檔案流合并(用檔案流方法實體化抽象流):</summary>
function TServerMethods1.mergeFilesUseTFileStream(const toFilePath:string): Boolean;
var filesCount :Integer; SearchRec :TSearchRec; currPath :string;
fileName :string; filesName :TStringList;
FileTStream,tmpTStream :TStream;
begin
TDirectory.SetCurrentDirectory(System.IOUtils.TPath.GetLibraryPath);
fileName:=System.IOUtils.TPath.GetLibraryPath + toFilePath;
if TDirectory.Exists(TDirectory.GetParent(fileName)) then
begin
currPath:=TDirectory.GetParent(fileName);
//強制路徑產生://ForceDirectories(currPath)
//DirectoryExists:路徑是否存在//FileExists:檔案是否存在:uses System.SysUtils;
TDirectory.SetCurrentDirectory(currPath);
//:進入該路徑(設定當前路徑并進入)
if FileExists(fileName)=true then TFile.Delete(fileName);
end;
filesCount := FindFirst(currPath + '\' + '*.tmp', faAnyFile, SearchRec);//:搜索成功回傳0
if filesCount<>0 then
begin
System.SysUtils.FindClose(SearchRec);//?不識別:需要帶上前綴:System.SysUtils.
TDirectory.SetCurrentDirectory(System.IOUtils.TPath.GetLibraryPath);
Result :=false; exit;
end;
FileTStream:=TFileStream.Create(fileName,fmCreate);
filesName:=TStringList.Create;
while filesCount = 0 do
begin
filesName.Add(SearchRec.Name);
tmpTStream:=TFileStream.Create(SearchRec.Name,fmOpenRead);
try
//Vcl.Forms.Application.ProcessMessages;
//:不卡界面:VCL將執行緒池TServerMethods1的訊息事件交給應用的主執行緒
//:不卡界面:FMX用執行緒同步方法TThread.Synchronize
FileTStream.CopyFrom(tmpTStream,0);
filesCount := FindNext(SearchRec);
finally
tmpTStream.disposeOf;
end;
end;
try
for filesCount := 0 to filesName.Count-1 do
begin
if FileExists(filesName[filesCount].Trim)=true then
TFile.Delete(filesName[filesCount].Trim);
end;
finally
System.SysUtils.FindClose(SearchRec);//?不識別:需要帶上前綴:System.SysUtils.
filesName.disposeOf; FileTStream.disposeOf;
TDirectory.SetCurrentDirectory(System.IOUtils.TPath.GetLibraryPath);
end;
Result :=true;
end;
本博客關聯文章:原始碼下載:https://download.csdn.net/download/pulledup/12578989
《TTreeView完整的列舉和遞回演算法》 https://blog.csdn.net/pulledup/article/details/103687816
喜歡的話,就在下面點個贊、收藏就好了,方便看下次的分享:
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/81636.html
標籤:其他
下一篇:FPGA——數字電路崛起的新星
