【发布时间】:2015-03-17 21:21:09
【问题描述】:
我正在尝试获取 PDF 文档并通过 MVC 网站将其上传以存储到 SAP 结构中。 SAP 结构要求将字节数组分成 1022 个长度的部分。该程序似乎运行良好,直到我尝试从 SAP 中查看 PDF 文档。不幸的是,由于访问权限,我无法查看存储在 SAP 中的 PDF 数据。因此,我创建了一种 MOCK 程序来匹配发送到 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;
}
【问题讨论】:
-
不应该是这个:
prevLength = item.Length;是这个吗?prevLength += item.Length;(注意在=运算符之前添加了+符号)。在我看来,好像您一遍又一遍地覆盖目标数组的开头。 -
这是错误。您如何看待将其添加为答案而不是评论以便我可以标记它?
-
发布了更完整的答案。