-2

我正在尝试用 java 构建我的简单网络服务器。所以我在教程中找到了这段代码,
我试图理解这种方法的作用,但我无法解释 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);
                }   
                    }
            } 
4

4 回答 4

0

你的问题不清楚。

如果您一般询问 BufferedReader 类,您应该从类参考文档、Oracle 的教程和一点谷歌搜索开始。

于 2013-08-11T07:26:27.107 回答
0

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

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

于 2013-08-11T07:27:26.280 回答
0

我已经把代码改写了一点,并添加了更多评论,希望这会有所帮助

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()); 
           }
于 2013-08-11T07:33:04.790 回答
0

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

  • 此方法正在解析请求并对其进行响应。
  • 出于某种原因,他不支持网站图标,因此在没有检查是否存在的情况下以未找到作为响应。这不是一个功能齐全的 Web 服务器

见 * http://www.jmarshall.com/easy/http/ * http://www.tutorialspoint.com/http/ * http://www.w3.org/Protocols/rfc2616/rfc2616.html

  • 大多数 Web 服务器都是用 C++ 编写的,但你可以用 Java 编写一个用于每小时获得几百次点击的节点。
于 2013-08-11T07:42:45.980 回答