Java Web服务器

Java Web服务器,java,networking,web,Java,Networking,Web,我正在尝试用java构建我的简单Web服务器。所以我在教程中找到了这段代码 我试图理解这种方法的作用,但我无法解释 BufferedReader的用途是什么以及他为什么使用它 public void run() { BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream())); String line = reader.read

我正在尝试用java构建我的简单Web服务器。所以我在教程中找到了这段代码
我试图理解这种方法的作用,但我无法解释 BufferedReader的用途是什么以及他为什么使用它

public void run()
{
            BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));

            String line = reader.readLine();
            String filename = "";
            File file = null;

            String parts[] = line.split(" ");

            if (parts.length != 3)
            {
                return;
            }

            if (parts[0].compareTo("GET") == 0)
            {
                filename = parts[1].substring(1);

                if (filename.equals("favicon.ico") == true)
                {
                    System.out.println("404 File Not Found!");
                    return;
                }

                else
                {
                    System.out.println(filename);
                }
            }


            printToBrowser(bos, "HTTP/1.1 200 OK");
            printToBrowser(bos, "");

            if (filename.compareTo("") == 0)
            {
                file = new File("index.txt");
            }

            else
            {
                file = new File(filename);
            }

            try
            {   
                FileInputStream input = new FileInputStream(file);
                int a; 
                byte[] buffer = new byte[1024];

                while ((a = input.read(buffer, 0, 1024)) > 0)
                {   
                    sleep(100);
                    bos.write(buffer, 0, a);
                }   
                    }
            } 

你的问题不清楚


如果您询问BufferedReader类的一般情况,应该从、Oracle和一点开始。

这里BufferedReader用于从客户端读取请求。你的问题是为什么BufferedReader


BufferedReader
的优点是:它速度快,允许我们读取整行而不是字符,这意味着它在整行上迭代。如果不使用BufferedReader,我们就必须编写额外的代码,通过连接字符来创建字符串,然后将其拆分以进一步处理,这非常耗时。

我已经将代码重写了一点,并添加了更多注释,希望这能有所帮助

public void run()
{
           //Create a reader to read the request from client
            BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));

           //from client get the line
            String line = reader.readLine();

            //file name client sends or use default one
            String filename = "";

            //file to read
            File file = null;

           //split the request with (space) as separator
            String parts[] = line.split(" ");

           // if we don't have exactly 3 words, return
            if (parts.length != 3) {
                return;
            }

           // if first part is GET
            if (parts[0].compareTo("GET") == 0) {
                //second word is the file name
                filename = parts[1].substring(1);

                //if filename is favicon.ico then print 404 not found
                if (filename.equals("favicon.ico")) {
                    System.out.println("404 File Not Found!");
                    return;
                } else {
                    //else print the file name
                    System.out.println(filename);
                }
            }

           //tell the requiting client the request was successful
            printToBrowser(bos, "HTTP/1.1 200 OK");
            //i think this is to indicate end of line
            printToBrowser(bos, "");

            if (filename.equals("")) { 
                file = new File("index.txt"); //if there is no file name set the file to index.txt
            } else {
                file = new File(filename); //else of the file name
            }

            try {   
                FileInputStream input = new FileInputStream(file); //read the file
                int a; 
                byte[] buffer = new byte[1024]; //create a buffer of 1024 bytes could also use BufferedReader

                while ((a = input.read(buffer, 0, 1024)) > 0) { //read the file   
                    sleep(100); //sleep, not sure why this is here
                    bos.write(buffer, 0, a); //send the file to client
                }   

            } catch (Exception e) {
                //TODO : handle the error more gracefully, also indicate to client
                System.err.println(e.getMessage()); 
           }

缓冲读取器正在读取请求。它缓冲了它的性能。您应该阅读HTTP协议-第一行如何指示操作类型、标头是什么、重要标头、正文、web表单值如何在请求中发送等

  • 此方法正在解析请求并响应它
  • 由于某些原因,他不支持favicon,因此在未检查是否存在favicon的情况下,会以not found作为响应。这不是一个功能齐全的web服务器
看 * * *

    大多数Web服务器是用C++编写的,但是java中有一个节点可以得到一个100个小时的点击。
旁注:在
中,如果(filename.equals(“favicon.ico”)==true)
,则不需要
==true
<代码>如果(filename.equals(“favicon.ico”)足够了(而且更好)。@maroun maroun我同意,但更好是有争议的。我知道还有一些聪明高效的程序员,他们只是喜欢添加真实的部分,而讨厌!xBoolVar,首选xBoolVar==false。请参阅示例代码。您的主要问题是“试图理解此方法的作用”吗?这是web服务器响应的核心。他还添加了这个方法。你能给我解释一下私有void printToBrowser(BufferedOutputStream bos,String s)在做什么吗{try{String news=s+“\r\n”;byte[]array=news.getBytes();for(int i=0;i