我试图采取一个PDF文档,并通过MVC网站上传它,以存储到SAP结构。SAP结构要求将字节数组分解为1022个长度区段。这个程序似乎运行得很好,以至于我试图从SAP中查看PDF文档。不幸的是,由于访问权限,我无法查看存储在SAP中的PDF数据。因此,我创建了一种模拟程序,以匹配字节数组发送到SAP (fileContent)之前的情况,以及一旦从SAP (fileContentPostSAP)返回后它应该是什么样的。
该程序比较字节数组,并在数组位置1022处查找不匹配值。
我的程序中是否有导致字节数组不匹配的错误?他们应该完全匹配,对吧?
ClaimsIdentityMgr claimIdentityMgr = new ClaimsIdentityMgr();
ClaimsIdentity currentClaimsIdentity = claimIdentityMgr.GetCurrentClaimsIdentity();
var subPath = "~/App_Data/" + currentClaimsIdentity.EmailAddress;
var destinationPath = Path.Combine(Server.MapPath(subPath), "LG WM3455H Spec Sheet.pdf");
byte[] fileContent = System.IO.File.ReadAllBytes(destinationPath);
//pretend this is going to SAP
var arrList = SAPServiceRequestRepository.CreateByteListForStructure(fileContent);
var mockStructureList = new List<byte[]>();
foreach (byte[] b in arrList)
mockStructureList.Add(b);
//now get it back from Mock SAP
var fileContentPostSAP = new byte[fileContent.Count()];
var rowCounter = 0;
var prevLength = 0;
foreach (var item in mockStructureList)
{
if (rowCounter == 0)
System.Buffer.BlockCopy(item, 0, fileContentPostSAP, 0, item.Length);
else
System.Buffer.BlockCopy(item, 0, fileContentPostSAP, prevLength, item.Length);
rowCounter++;
prevLength = item.Length;
}
//compare the orginal array with the new one
var areEqual = (fileContent == fileContentPostSAP);
for (var i = 0; i < fileContent.Length; i++)
{
if (fileContent[i] != fileContentPostSAP[i])
throw new Exception("i = " + i + " | fileContent[i] = " + fileContent[i] + " | fileContentPostSAP[i] = " + fileContentPostSAP[i]);
}下面是CreateByteListForStructure函数:
public static List<byte[]> CreateByteListForStructure(byte[] fileContent)
{
var returnList = new List<byte[]>();
for (var i = 0; i < fileContent.Length; i += 1022)
{
if (fileContent.Length - i >= 1022)
{
var localByteArray = new byte[1022];
System.Buffer.BlockCopy(fileContent, i, localByteArray, 0, 1022);
returnList.Add(localByteArray);
}
else
{
var localByteArray = new byte[fileContent.Length - i];
System.Buffer.BlockCopy(fileContent, i, localByteArray, 0, fileContent.Length - i);
returnList.Add(localByteArray);
}
}
return returnList;
}发布于 2015-03-17 21:30:16
代码中似乎有一个简单的bug。
此循环从块中重新构造数组的内容:
var prevLength = 0;
foreach (var item in mockStructureList)
{
if (rowCounter == 0)
System.Buffer.BlockCopy(item, 0, fileContentPostSAP, 0, item.Length);
else
System.Buffer.BlockCopy(item, 0, fileContentPostSAP, prevLength, item.Length);
rowCounter++;
prevLength = item.Length;
}通过对块的描述,每个块都是1022字节,这意味着在第一次迭代之后,prevLength被设置为1022,但是在下一次迭代之后,它又被设置为1022。
对prevLength的更正确的分配是:
prevLength += item.Length;
^
|
+-- added this这将正确地将输出数组中的指针一次向前移动一个块,而不是将指针移动到第二个块,然后将其留在那里。
基本上,您可以将块0写在正确的位置,但是所有其他块都在块1的顶部,在输出数组中留下块2和其后为零。
https://stackoverflow.com/questions/29109946
复制相似问题