C#HttpListener:存储发布的文件总是比原始文件大2个字节 [英] C# HttpListener: storing posted file is always 2 byte larger than original
问题描述
问题描述:
我的C#程序中有一个HttpListener
,它接受带有已发布文件(jpg,doc,zip或类似文件)的POST请求.我正在同一台Windows计算机上发送和接收文件.
I have an HttpListener
within my C# program that takes a POST request with a posted file (jpg, doc, zip or alike). I am sending and receiving the file on the same Windows machine.
我从 https://stackoverflow.com/a/8468520/241475 (来自Java世界,对于C#/.Net),据说只是一种概念证明,并且在服务器端上传和存储数据几乎可以正常工作,除了存储的文件始终比原始文件大2个字节.
I took the code from https://stackoverflow.com/a/8468520/241475 (coming from the Java world, I'm fairly new to C#/.Net), which is said to just be a proof of concept, and the upload and storing of data on the server side almost works fine, except that the stored files is always 2 bytes larger than the original.
这对于jpg,doc或单个zip文件无关紧要-我仍然可以打开它们并且它们显示得很好-但是当我上载属于多卷zip的zip文件时,对于其中的一个文件,解压缩通常会失败拉链,其余的则解压.
This does not matter for jpg, doc or single zip files - I can still open them and they display fine - but when I upload a zip file that is part of a multi-volume zip, unzipping usually fails for one file inside the zip, while the rest gets unzipped fine.
示例:
在此示例中,您可以看到原始文件(import_filesystem.kmumitkst.z01.ORIGINAL
),其大小为10485760
个字节,我将其发布到HttpListener中,并存储了名为import_filesystem.kmumitkst.z01
的版本,该文件的大小为10485762
个字节
In this example, you can see the original file (import_filesystem.kmumitkst.z01.ORIGINAL
), which was 10485760
bytes large, which I posted to my HttpListener and which stored the version named import_filesystem.kmumitkst.z01
, which is 10485762
bytes large.
然后我通过WinRar解压缩时,它告诉我CRC/校验和不正确,并且其中的一个文件由于这两个额外的字节而损坏.
When I then unzip it via WinRar, it tells me that the CRC/checksum isn't correct and one file in it is corrupt due to these two extra bytes.
代码:
这是存储接收到的文件的代码:
This is the code storing the received file:
while (_listener.IsListening)
{
ThreadPool.QueueUserWorkItem((c) =>
{
var ctx = c as HttpListenerContext;
SaveFile(ctx.Request.ContentEncoding, GetBoundary(ctx.Request.ContentType),
ctx.Request.InputStream, targetFilePath);
...
}
}
// =====================================
private static void SaveFile(Encoding enc, String boundary, Stream input, string targetPath)
{
Byte[] boundaryBytes = enc.GetBytes(boundary);
Int32 boundaryLen = boundaryBytes.Length;
using (FileStream output = new FileStream(targetPath, FileMode.Create, FileAccess.Write))
{
Byte[] buffer = new Byte[1024];
Int32 len = input.Read(buffer, 0, 1024);
Int32 startPos = -1;
// Find start boundary
while (true)
{
if (len == 0)
{
throw new Exception("Start Boundaray Not Found");
}
startPos = IndexOf(buffer, len, boundaryBytes);
if (startPos >= 0)
{
break;
}
else
{
Array.Copy(buffer, len - boundaryLen, buffer, 0, boundaryLen);
len = input.Read(buffer, boundaryLen, 1024 - boundaryLen);
}
}
// Skip four lines (Boundary, Content-Disposition, Content-Type, and a blank)
for (Int32 i = 0; i < 4; i++)
{
while (true)
{
if (len == 0)
{
throw new Exception("Preamble not Found.");
}
startPos = Array.IndexOf(buffer, enc.GetBytes("\n")[0], startPos);
if (startPos >= 0)
{
startPos++;
break;
}
else
{
len = input.Read(buffer, 0, 1024);
}
}
}
Array.Copy(buffer, startPos, buffer, 0, len - startPos);
len = len - startPos;
while (true)
{
Int32 endPos = IndexOf(buffer, len, boundaryBytes);
if (endPos >= 0)
{
if (endPos > 0) output.Write(buffer, 0, endPos);
break;
}
else if (len <= boundaryLen)
{
throw new Exception("End Boundaray Not Found");
}
else
{
output.Write(buffer, 0, len - boundaryLen);
Array.Copy(buffer, len - boundaryLen, buffer, 0, boundaryLen);
len = input.Read(buffer, boundaryLen, 1024 - boundaryLen) + boundaryLen;
}
}
}
}
private static String GetBoundary(String ctype)
{
return "--" + ctype.Split(';')[1].Split('=')[1];
}
private static Int32 IndexOf(Byte[] buffer, Int32 len, Byte[] boundaryBytes)
{
for (Int32 i = 0; i <= len - boundaryBytes.Length; i++)
{
Boolean match = true;
for (Int32 j = 0; j < boundaryBytes.Length && match; j++)
{
match = buffer[i + j] == boundaryBytes[j];
}
if (match)
{
return i;
}
}
return -1;
}
问题:
这两个额外的字节从哪里来?不管我发送哪种文件或文件大小,它总是恰好2个字节.
Where do these two extra bytes come from? Regardless of what kind of file or file size I sent, it's always exactly 2 bytes too much.
还有其他方法可以通过HttpListener
保存接收到的文件吗?感谢您的提示.
Is there any other way to save a received file through the HttpListener
? Thanks for any hint.
推荐答案
找到解决方案,必须进行更改
Found the solution, had to change
while (true)
{
Int32 endPos = IndexOf(buffer, len, boundaryBytes);
if (endPos >= 0)
{
if (endPos > 0) output.Write(buffer, 0, endPos);
break;
}
到
while (true)
{
Int32 endPos = IndexOf(buffer, len, boundaryBytes);
if (endPos >= 0)
{
if (endPos > 0) output.Write(buffer, 0, endPos-2);
break;
}
在endPos
上添加-2
,以避免在末尾出现额外的换行符.
adding a -2
to the endPos
, to avoid extra line break at the end.
这篇关于C#HttpListener:存储发布的文件总是比原始文件大2个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!