無(wú)奈之下,想到了一個(gè)下下策,那就是通過(guò)分析接受到的輸入流,從中提取想要的數(shù)據(jù)。比如我在上文中提到的數(shù)據(jù)(兩個(gè)參數(shù),一個(gè)圖片文件)

實(shí)現(xiàn)思路:讀取輸入流,通過(guò)非文件部份的分析,確定文件在整個(gè)流中的位置和大小,再對(duì)輸入流重新讀取以得到文件。
看上面的POST數(shù)據(jù),參數(shù)部分及分隔符等,說(shuō)白了都是些字符串。基本上都是英文數(shù)字符號(hào)等,如有中文發(fā)送之前可做一下編碼,這樣基本上可以確保不會(huì)因編碼問(wèn)題造成計(jì)算失誤(英文字符各編碼都是相同的)。這一部分內(nèi)容(非文件部分,文件部分是二進(jìn)制格式來(lái)的,千萬(wàn)別這樣去做)可以得到。
通過(guò)接受輸入的流轉(zhuǎn)成字符串,如果參數(shù)中有中文,請(qǐng)注意客戶端POST時(shí)使用何種編碼:
復(fù)制代碼 代碼如下:
byte[] input = Request.BinaryRead(Request.TotalBytes);
string source = Encoding.UTF8.GetString(input);
復(fù)制代碼 代碼如下:
Regex rginput = new Regex("略");
if (rginput.IsMatch(source))
{
int headLength=Encoding.UTF8.GetBytes(rginput.Match(source).Value)
}
復(fù)制代碼 代碼如下:
//保存文件
FileStream fss = new FileStream("path", FileMode.Create);
fss.Write(input, headLength, input.Length-headLength-footLength);
fss.Close();
復(fù)制代碼 代碼如下:
//得到文件字節(jié)數(shù)組
byte[] imgcont = new byte[input.Length - headLength-footLength];
MemoryStream ms = new MemoryStream(input);
//光標(biāo)移動(dòng)到文件開始處
ms.Seek(headLength,SeekOrigin.Begin);
ms.Read(imgcont, 0, imgcont.Length);
ms.Close();
新聞熱點(diǎn)
疑難解答
圖片精選