Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        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);
            }
        }
Ejemplo n.º 3
0
        private void UploadFile(HttpListenerContext ctx)
        {
            String fileId = String.Empty;

            Byte[] buffer = GetFile(ctx);

            using (BigFile bigFile = new BigFile(this.FilePath, this.FileSize))
            {
                fileId = bigFile.Write(buffer).ToString();
            }
            buffer = null;
            String fileUrl = GetApiUrl(ctx.Request.LocalEndPoint.Address.ToString(), ctx.Request.LocalEndPoint.Port) + "/" + fileId;

            Trace.Write(fileUrl);

            using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
            {
                writer.Write(fileUrl);

                ctx.Response.AppendHeader("File-Id", fileId);
                ctx.Response.StatusCode  = (Int32)HttpStatusCode.OK;
                ctx.Response.ContentType = "text/plain";
                writer.Close();
            }
        }
Ejemplo n.º 4
0
        private static void TestWrite()
        {
            var bigFile = new BigFile(BIG_FILE_NAME, 10737418240);

            for (int i = 0; i < 10000000; i++)
            {
                var index        = i % 10;
                var fileName     = index.ToString() + ".jpg";
                var buffer       = File.ReadAllBytes(fileName);
                var bufferLength = buffer.Length;
                var offset       = bigFile.Write(buffer);
                var msg          = string.Format("{0},{1},{2},{3}\n", offset, bufferLength, fileName, i);
                WriteMsg(BIG_FILE_NAME + ".txt", msg);

                Console.Write(msg);
            }
        }
Ejemplo n.º 5
0
        private void StartInfo(HttpListenerContext ctx)
        {
            const Int32 MB_SIZE = 1048576;
            Process     process = Process.GetCurrentProcess();

            PerformanceCounter pcMemory  = new PerformanceCounter("Process", "Working Set - Private", process.ProcessName);
            PerformanceCounter pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");

            pcCpuLoad.NextValue();

            CounterSample cs1 = pcCpuLoad.NextSample();

            Thread.Sleep(200);
            CounterSample cs2             = pcCpuLoad.NextSample();
            Single        finalCpuCounter = CounterSample.Calculate(cs1, cs2);

            HostConfig section = ConfigurationManager.GetSection(HOST_CONFIG_SECTION) as HostConfig;

            using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream, Encoding.UTF8))
            {
                writer.WriteLine("Web文件传输服务已经启动!");
                writer.WriteLine("启动时间:" + process.StartTime.ToString(TIME_FORMAT));
                writer.WriteLine("系统时间:" + DateTime.Now.ToString(TIME_FORMAT));
                writer.WriteLine("请求地址:" + ctx.Request.RawUrl);
                writer.WriteLine("客户端IP:" + ctx.Request.RemoteEndPoint.Address);
                writer.WriteLine("线程数量:" + process.Threads.Count);
                writer.WriteLine("程序内存:" + Math.Round(pcMemory.NextValue() / MB_SIZE, 2) + "MB");
                writer.WriteLine("系统CPU:" + Math.Round(finalCpuCounter, 0) + "%");
                writer.WriteLine("OS版本:" + Environment.OSVersion.VersionString);

                if (section != null && section.Hosts != null)
                {
                    writer.WriteLine();
                    foreach (HostElement setting in section.Hosts)
                    {
                        writer.WriteLine("API:" + GetApiUrl(ctx.Request.LocalEndPoint.Address.ToString(), ctx.Request.LocalEndPoint.Port));
                        writer.WriteLine("文件路径:" + setting.GetFilePath() + " 文件大小:" + setting.Size + " Http端口:" + setting.Port);

                        BigFile.Header header = null;
                        using (BigFile file = new BigFile(setting.GetFilePath(), setting.FileSize))
                        {
                            header = file.GetHeader();
                        }

                        writer.WriteLine("剩余容量:" + Math.Round((Decimal)header.FreeStorage / MB_SIZE, 2) + "MB");
                        writer.WriteLine("文件个数:" + header.FileCount);
                        writer.WriteLine("覆盖前文件个数:" + header.CycleTotalFileCount + " 覆盖次数:" + header.OverwriteCount + " 覆盖时间:" + header.OverwriteTime.ToString(TIME_FORMAT));
                        writer.WriteLine("上一文件刻度:" + header.PrevOffset + " 上一文件存储时间:" + header.ActiveTime.ToString(TIME_FORMAT));

                        if (header.FinalOffset > 0)
                        {
                            writer.WriteLine("末尾文件刻度:" + header.FinalOffset + " 末尾文件存储时间:" + header.FinalFileTime.ToString(TIME_FORMAT));
                        }

                        writer.WriteLine("当前文件刻度:" + header.CurrentOffset);
                        writer.WriteLine();
                    }
                }

                ctx.Response.StatusCode      = (Int32)HttpStatusCode.OK;
                ctx.Response.ContentType     = "text/plain";
                ctx.Response.ContentEncoding = Encoding.UTF8;
                writer.Close();
            }
        }