乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      讀取上傳數(shù)據(jù)流(1)

       悟靜 2012-02-16

      讀取上傳數(shù)據(jù)流(1)

      在 POST 方式的請求中,請求參數(shù)通過請求的 Body 部分提供,在多部分形式的請求中,通過邊界來劃分請求參數(shù)。在這一部分的代碼中,主要是計算好邊界的位置。為了便于讀取上傳的文件,MultipartStream 流專門用于讀取多部分組成的數(shù)據(jù),如代碼清單2-7所示。

      代碼清單2-7 讀取上傳數(shù)據(jù)流

      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Text;  
      5.  
      6. using System.IO;  
      7. using System.Web;  
      8. using System.Text.RegularExpressions;  
      9.  
      10. namespace DiskFileUpload  
      11. {  
      12.     public class MultipartStream : Stream  
      13.     {  
      14.         private System.Text.Encoding bodyEncoding =
        System.Text.Encoding.UTF8;  
      15.  
      16.         private static int HEADER_PART_SIZE_MAX = 4096;  
      17.  
      18.         private static byte[] HEADER_SEPARATOR = 
        new byte[] { 0xd, 0xa, 0xd, 0xa };  
      19.  
      20.         // 匹配參數(shù)名稱的正則表達(dá)式 . 表示除換行之外的所有字符  
      21.         // +? 表示懶惰模式  
      22.         private static readonly System.Text.
        RegularExpressions.Regex 
        nameRegex 
      23.             = new System.Text.RegularExpressions.
        Regex("
        name=\"(.+?)\"");  
      24.  
      25.         // 匹配上傳文件名的正則表達(dá)式  
      26.         private static readonly System.Text.
        RegularExpressions.Regex 
        filenameRegex 
      27.             = new System.Text.RegularExpressions.
        Regex("
        filename=\"(.+)\"");  
      28.  
      29.         private static readonly System.Text.
        RegularExpressions.Regex 
        contentTypeRegex 
      30.             = new Regex("Content-Type=\"(.+)\"");  
      31.  
      32.         private byte[] boundary;  
      33.         private int boundaryLength;  
      34.  
      35.         int[] next;  
      36.  
      37.         // 流中內(nèi)容的字節(jié)長度  
      38.         private long length;  
      39.         public override long Length  
      40.         {  
      41.             get { return this.length; }  
      42.         }  
      43.  
      44.         // 當(dāng)前位置  
      45.         public override long Position  
      46.         {  
      47.             get { return this.startIndex + this.bufferStart; }  
      48.             set { throw new Exception("不允許設(shè)置當(dāng)前位置"); }  
      49.         }  
      50.  
      51.         // 內(nèi)部預(yù)讀的緩沖區(qū)  
      52.         private byte[] buffer;  
      53.  
      54.         // 緩沖內(nèi)容的起始位置  
      55.         private int startIndex;  
      56.         private int bufferLength;  
      57.  
      58.         // 緩沖區(qū)也不一定填滿,緩沖區(qū)有內(nèi)容部分的起始下標(biāo)  
      59.         private int bufferStart;  
      60.         private int bufferEnd;  
      61.  
      62.         private IFileUpload worker;  
      63.  
      64.         #region KMP  
      65.  
      66.         /// <summary> 
      67.         /// 構(gòu)造 KMP 的匹配模式數(shù)組  
      68.         /// 對于 KMP 來說,函數(shù)表示包括當(dāng)前字符的字串與整個模式串匹配的數(shù)量  
      69.         /// 0 表示當(dāng)前位置無匹配  
      70.         ///  
      71.         /// </summary> 
      72.         /// <param name="pattern"></param> 
      73.         /// <returns></returns> 
      74.         private int[] BuildKMP(byte[] pattern)  
      75.         {  
      76.             int[] next = new int[pattern.Length];  
      77.  
      78.             next[0] = 0;  
      79.  // 第一個位置一定為 0  
      80.  
      81.             int j = 0;  
      82.  // 匹配的起始位置  
      83.             for (int i = 1; i < pattern.Length; i++)  
      84.             {  
      85.                 // 如果已經(jīng)匹配上,但是現(xiàn)在不能匹配,回溯尋找  
      86.                 while (j > 0 && pattern[j] != pattern[i])  
      87.                 {  
      88.                     j = next[j - 1];  
      89.                 }  
      90.  
      91.                 // 如果能夠匹配上,向下推進(jìn)一個位置  
      92.                 // 注意 i 在 for 循環(huán)中自動推進(jìn)  
      93.                 if (pattern[j] == pattern[i])  
      94.                     j++;  
      95.  
      96.                 // 保存  
      97.                 next[i] = j;  
      98.             }  
      99.             return next;  
      100.         }  
      101.  
      102.         #endregion  
      103.  
      104.         public MultipartStream(IFileUpload worker, string boundaryString,  
      105.             int bufferSize)  
      106.         {  
      107.             boundaryString = "\r\n--" + boundaryString;  
      108.             this.boundary = System.Text.Encoding.ASCII.GetBytes(boundaryString);  
      109.             this.boundaryLength = boundary.Length;  
      110.             thisthis.next = this.BuildKMP(this.boundary);  
      111.  
      112.             this.worker = worker;  
      113.  
      114.             // 設(shè)置流的總長度  
      115.             this.length = worker.Length;  
      116.  
      117.             // 預(yù)讀  
      118.             this.startIndex = 0;  
      119.  
      120.             // 緩沖區(qū)大小  
      121.             this.bufferLength =  
      122.                 System.Math.Max(worker.PreloadLength, 64 * 1024);  
      123.  
      124.             this.buffer = new byte[this.bufferLength];  
      125.             this.bufferStart = 0;  
      126.             this.bufferEnd = worker.GetPreloadedBody(this.buffer, 0);  
      127.         }  
      128.  
      129.         // 是否允許讀取  
      130.         public override bool CanRead  
      131.         {  
      132.             get { return true; }  
      133.         }  
      134.  
      135.         // 是否允許定位  
      136.         public override bool CanSeek  
      137.         {  
      138.             get { return false; }  
      139.         }  
      140.  
      141.         // 是否允許寫入  
      142.         public override bool CanWrite  
      143.         {  
      144.             get { return false; }  
      145.         }  
      146.  
      147.         public override void Flush()  
      148.         {  
      149.             throw new NotImplementedException();  
      150.         }  
      151.  
      152.         public override int ReadByte()  
      153.         {  
      154.             // 是否已經(jīng)讀取到了最后  
      155.             if (this.startIndex + this.bufferStart >= this.length)  
      156.                 return -1;  
      157.  
      158.             // 希望讀取的內(nèi)容是否在緩沖區(qū)中  
      159.             if( this.bufferStart == this.bufferEnd)  
      160.             {  
      161.                 // 已經(jīng)超出邊界,必須繼續(xù)進(jìn)行讀取  
      162.                 int count = this.MakeAvailable();  
      163.             }  
      164.             int result =  this.buffer[this.bufferStart];  
      165.             this.bufferStart++;  
      166.  
      167.             return result;  
      168.         } 

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約