用java编写简单Webserver,理解webserver的功能。

代码来自网上。

1.文件处理类 processer.java

复制代码
 1 package webbook.chapter2;
 2 
 3 import java.io.*;
 4 import java.net.Socket;
 5 
 6 /**
 7  * 处理一个HTTP用户请求的线程类。
 8  */
 9 public class Processor extends Thread {
10     private PrintStream out;
11     private InputStream input;
12     /** 默认的服务器存放访问内容的目录 */
13     public static final String WEB_ROOT = "/library/webserver/documents";
14 
15     public Processor(Socket socket) {
16         try {
17             input = socket.getInputStream();
18             out = new PrintStream(socket.getOutputStream());
19         } catch (IOException e) {
20             e.printStackTrace();
21         }
22     }
23 
24     public void run() {
25         try {
26             String fileName = parse(input);
27             readFile(fileName);
28         } catch (IOException e) {
29             e.printStackTrace();
30         }
31     }
32 
33     /**
34      * 解析客户机发过的所有HTTP请求,如果是符合HTTP协议内容的, 就分析出客户机所要访问文件的名字,并且返回文件名。
35      */
36     public String parse(InputStream input) throws IOException {
37         BufferedReader in = new BufferedReader(new InputStreamReader(input));
38         String inputContent = in.readLine();
39         if (inputContent == null || inputContent.length() == 0) {
40             sendError(400, "Client invoke error");
41             return null;
42         }
43         // 分析客户请求的HTTP信息,分析出到底想访问哪个文件,
44         // 发过来的HTTP请求应该是三部分。
45 
46         String request[] = inputContent.split(" ");
47         if (request.length != 3) {
48             sendError(400, "Client invoke error");
49             return null;
50         }
51         // 第一部分是请求的方法
52         String method = request[0];
53         // 第二部分是请求的文件名
54         String fileName = request[1];
55         // 第三部分是HTTP版本号
56         String httpVersion = request[2];
57         System.out.println("Method: " + method + ", file name: " + fileName + ", HTTP version: " + httpVersion);
58         return fileName;
59     }
60 
61     /**
62      * 处理调用一个文件的请求
63      */
64     public void readFile(String fileName) throws IOException {
65         File file = new File(Processor.WEB_ROOT + fileName);
66         if (!file.exists()) {
67             sendError(404, "File Not Found");
68             return;
69         }
70         // 把文件的内容读取到in对象中。
71         InputStream in = new FileInputStream(file);
72         byte content[] = new byte[(int) file.length()];
73         in.read(content);
74         out.println("HTTP/1.0 200 sendFile");
75         out.println("Content-length: " + content.length);
76         out.println();
77         out.write(content);
78         out.flush();
79         out.close();
80         in.close();
81     }
82 
83     /**
84      * 发送错误的信息
85      */
86     public void sendError(int errNum, String errMsg) {
87         out.println("HTTP/1.0 " + errNum + " " + errMsg);
88         out.println("Content-type: text/html");
89         out.println();
90         out.println("<html>");
91         out.println("<head><title>Error " + errNum + "--" + errMsg + "</title></head>");
92         out.println("<h1>" + errNum + " " + errMsg + "</h1>");
93         out.println("</html>");
94         out.println();
95         out.flush();
96         out.close();
97     }
98 }
复制代码

 

2.webserver.java

复制代码
 1 package webbook.chapter2;
 2 
 3 import java.io.IOException;
 4 import java.net.ServerSocket;
 5 import java.net.Socket;
 6 
 7 public class WebServer {
 8     /** 默认使用的服务器Socket端口号 */
 9     public static final int HTTP_PORT = 8080;
10     private ServerSocket serverSocket;
11 
12     public void startServer(int port) {
13         try {
14             serverSocket = new ServerSocket(port);
15             System.out.println("Web Server startup on  " + port);
16             while (true) {
17                 Socket socket = serverSocket.accept();
18                 // 通过线程的方式来处理客户的请求
19                 new Processor(socket).start();
20             }
21         } catch (IOException e) {
22             e.printStackTrace();
23         }
24     }
25 
26     /**
27      * WebServer类的启动方法,可以通过命令行参数指定当前Web服务器所使用的端口号。
28      */
29     public static void main(String[] argv) throws Exception {
30         WebServer server = new WebServer();
31         if (argv.length == 1) {
32             server.startServer(Integer.parseInt(argv[0]));
33         } else {
34             server.startServer(WebServer.HTTP_PORT);
35         }
36     }
37 }
复制代码

 

简单webserver 充当的作用就是接受客户端程序发过来的请求,根据请求内容转换的数据找到自己制定路径下的文件,将其文件转为流返回客服端,这个过程所用的协议为http。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值