我编写了客户端/服务器代码,通过 Delphi 编写的 WebService 在系统之间来回移动文件。由于服务前面网关的限制,我被迫将大文件拆分为多条消息。下面的代码在我的测试中经常起作用。然而,有时,在最后一条数据上,它似乎并没有进入结果文件。我有另一个版本的代码,我在每一步都添加了大量日志记录,以在 writeBuffer 调用之前和之后验证 FileStream 中的当前位置并获取中间文件的大小。该版本的代码似乎每次都有效,这让我觉得我可能遇到了某种时间问题。我应该在每次写入后进行 Flush on the Stream 还是类似的事情?
responseObj := DocSvc.getDocument(GetFileInHeader, GetFileIn);
while processingGetFile do
begin
chunkID := StrToInt(responseObj.ValidationResults[0].ValidationResultId);
if chunkID = -3 then
begin
processingGetFile := false;
break;
end;
if chunkID = -2 then
begin
if responseObj.opsResponseobj.status then
begin
if responseObj.opsResponseObj.document <> 'NONEWFILE' then
begin
if FileExists2(DesintationFilePath) then
DeleteFile(DesintationFilePath);
Zipfile := TFileStream.Create(DesintationFilePath,FmOpenReadWrite or FmShareDenyNone or fmCreate);
DecodedZipfile := DecodeString(responseObj.opsResponseobj.document);
Zipfile.WriteBuffer(Pointer(DecodedZipfile)^, length(DecodedZipfile));
iniTransmit.WriteString(‘DocumentSection’,string(FileID), responseObj.ValidationResults[0].ReasonCode);
FreeAndNil(Zipfile);
end;
result := true;
processingGetFile := false;
end
else
begin
//Log failure
end;
end
else if chunkID = -1 then
begin
Zipfile.Position := getFileSize(DesintationFilePath);
DecodedZipfile := DecodeString(responseObj.opsResponseObj.document);
Zipfile.WriteBuffer(Pointer(DecodedZipfile)^, Length(DecodedZipfile));
iniTransmit.WriteString(‘DocumentSection’,string(FileID), responseObj.ValidationResults[0].ReasonCode);
result := true;
processingGetFile := false;
end
else // in the middle of receiving pieces of a big file. Save off what we have, ask for more.
begin
if chunkID = 1 then
begin
if FileExists2(DesintationFilePath) then
DeleteFile(DesintationFilePath);
Zipfile := TFileStream.Create(DesintationFilePath,FmOpenReadWrite or FmShareDenyNone or fmCreate);
end;
Zipfile.Position := getFileSize(DesintationFilePath);
DecodedZipfile := DecodeString(responseObj.opsResponseObj.document);
Zipfile.WriteBuffer(Pointer(DecodedZipfile)^, Length(DecodedZipfile));
GetFileInHeader.messageFlowSequence := chunkID;
responseObj := DocSvc.getDocument(GetFileInHeader, GetFileIn);
end;
end;
function getFileSize(path: string): integer;
var
info : TWin32FileAttributeData;
begin
result := -1;
if not GetFileAttributesex(Pchar(path), GetFileExInfoStandard, @info) then
exit;
result := (info.nFileSizeLow or (info.nFileSizeHigh shl 32));
end;