private void DownloadFile(HttpListenerContext ctx) { Byte[] emptyBuffer = new Byte[0]; String fileIdUrl = ctx.Request.RawUrl.Replace(API_NAME, String.Empty).Trim('/'); Int64 fileId = 0L; Int64.TryParse(fileIdUrl, out fileId); using (BinaryWriter writer = new BinaryWriter(ctx.Response.OutputStream)) { if (fileId == 0) { writer.Write(emptyBuffer, 0, 0); } else { using (BigFile bigFile = new BigFile(this.FilePath, this.FileSize)) { Byte[] buffer = bigFile.Read(fileId) ?? emptyBuffer; writer.Write(buffer, 0, buffer.Length); } } ctx.Response.StatusCode = (Int32)HttpStatusCode.OK; ctx.Response.Close(); writer.Close(); } }
private static void TestRead() { var bigFile = new BigFile(BIG_FILE_NAME, 10737418240); var lines = ReadMsg(BIG_FILE_NAME + ".txt"); if (!Directory.Exists("Images")) { Directory.CreateDirectory("Images"); } var index = 0; foreach (var line in lines) { index++; if (string.IsNullOrEmpty(line)) { continue; } var arry = line.Split(','); var fileName = string.Format("{0}-{2}-{1}", arry[0], arry[2], Guid.NewGuid().ToString("N")); var filePath = Path.Combine("Images", fileName); var data = bigFile.Read(Convert.ToInt64(arry[0])); if (data == null || data.Length == 0) { Console.WriteLine("{0}/{1} {2} {3} 文件不存在", index, lines.Count, filePath, line); continue; } Console.WriteLine("{0}/{1} {2} {3}", index, lines.Count, filePath, line); File.WriteAllBytes(filePath, data); } }