【问题标题】:How would i be able to read and write files of different extensions c# and display them我如何能够读取和写入不同扩展名 c# 的文件并显示它们
【发布时间】:2019-11-29 02:53:56
【问题描述】:

我目前正在使用流读取器和写入器来读取文件并将其写入网页。目前 .html 文件正在工作,但 .jpg 和任何类型的图像都不会发布。

我如何能够提供像 mimetype 映射这样的文件?

namespace wdd_assign_07_github
{
    class Program
    {
     static void Main(string[] args)
        {
            //TcpListener server = null;

            if (args == null)
            {
                Console.WriteLine("args is null");

            }
            else if (args.Length != 3)
            {
                Console.WriteLine("Not enough arguments");
            }
            else
            {
                //do nothing
            }

            string webRoot = args[0].Substring(args[0].LastIndexOf("=") + 1);
            string webIP = args[1].Substring(args[1].LastIndexOf("=") + 1);
            string webPort = args[2].Substring(args[2].LastIndexOf("=") + 1);
            int numWebPort = Int32.Parse(webPort);
            Int32 port = numWebPort;

            TcpListener listener = new TcpListener(port);
            listener.Start();

            while (true)
            {
                //performing a blocking vall to accept requests
                Console.WriteLine("Waiting for a connection");
                TcpClient client = listener.AcceptTcpClient();

                StreamReader sr = new StreamReader(client.GetStream());
                StreamWriter sw = new StreamWriter(client.GetStream());

                try
                {
                    //client makes a request
                    //lets handle that request
                    string request = sr.ReadLine();

                    //lets print that out on our server console
                    Console.WriteLine(request);

                    //lets split that request
                    //[0]GET [1]PAGE [2]HTTPstring
                    string[] tokens = request.Split(' ');
                    string page = tokens[1];

                    //lets check if they requested the dedault page or not
                    if (page == "/")
                    {
                        page = "<H1>This is the default page</H1>";
                    }

                    //lets find the file now
                    StreamReader file = new StreamReader(webRoot + page);
                    sw.WriteLine("HTTP/1.0 200 OK\n");

                    //lets send the file
                    string data = file.ReadLine();

                    //while data doesnt equal null
                    while (data != null)
                    {
                        sw.WriteLine(data);
                        sw.Flush();
                        data = file.ReadLine();
                    }

                }
                catch (Exception e)
                {
                    //error
                    sw.WriteLine("HTTP/1.0 404 OK\n");
                    sw.WriteLine("<H1>SORRY! We couldnt find your file</H1>");
                    sw.Flush();
                }

                client.Close();
            }

        }
    }
}

【问题讨论】:

    标签: c# .net tcp stream


    【解决方案1】:

    您应该考虑多件事情:

    var mappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {
        {".jpg", "image/jpeg"},
        {".html", "text/html"},
        {".js", "application/javascript"},
        // ...
    };
    
    // later in your method
    var ext = Path.GetExtension(page);
    var contentType = mappings[ext]; // may throw if mapping is missing
    
    • 使用FileStream 而不是StreamReader(至少对于像jpg和png这样的二进制文件)

    【讨论】:

    • 顺便说一句,非常感谢您的回复,字典方法似乎是个好主意:)
    • @Mamooji: StreamReader 用于读取文本文件。甚至 HTML、JavaScript 和其他都是文本文件,你并不关心,你只是将字节复制到输出流中。所以FileStream 将适用于一切。
    • 顺便说一句 @Mamooji,您将通过支持我的回答来感谢我。 ?
    猜你喜欢
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    相关资源
    最近更新 更多