1. /** 
2.  * 用Java语言实现HTTP服务器,首先启动一个java.net.ServerSocket在提供服务的端口上监听连接.向客户返回文本时,可以用 
3.  * PrintWriter,但是如果返回二进制数据,则必须使用OutputStream.write(byte[])方法,返回的应答消息字符串可以使用 
4.  * String.getBytes()方法转换为字节数组返回,或者使用PrintStream的print()方法写入文本,用 
5.  * write(byte[])方法写入二进制数据. 
6.  *  
7.  * 以工程所在目录为web的根目录。 在工程的根目录下放一个大概如下的index.html 
8.  *  
9.  * <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
10.  * "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta 
11.  * http-equiv="Content-Type" content="text/html; charset=gbk"> 
12.  * <title>简单的测试</title> </head> <body> 你好!这是一个 简单的web服务器。<br> 
13.  * 这是一个图片!<br> 
14.  *  
15.  * <form action="/index.html"> <img alt="" src="images/test.gif"><br> 
16.  * 姓名:<input type="text" name="name" /><br> 
17.  * 密码:<input type="password" name="pass"></input><br> 
18.  * <input type="submit"/> </form> </body> </html> 
19.  *  
20.  * 放入图片位置: 工程根目录/images/test.gif <br> 
21.  * 打开浏览器输入http://localhost/index.html 可以展示index.html 
22.  *  
23.  * @author guazi 
24.  */
25. public class SimpleHttpServer implements
26.        
27. // 服务器Socket 
28.   
29.        
30. /** 
31.      * 服务器监听端口, 默认为 80. 
32.      */
33. public static int PORT = 80;// 标准HTTP端口 
34.   
35. /** 
36.      * 开始服务器 Socket 线程. 
37.      */
38. public
39. try
40. new
41. catch
42. "无法启动HTTP服务器:"
43.         }   
44. if (serverSocket == null)    
45. 1);// 无法开始服务器 
46.   
47. new Thread(this).start();    
48. "HTTP服务器正在运行,端口:"
49.     }   
50.   
51.        
52. /** 
53.      * 运行服务器主线程, 监听客户端请求并返回响应. 
54.      */
55. public void
56. while (true) {    
57. try
58. null;// 客户Socket 
59. // 客户机(这里是 IE 等浏览器)已经连接到当前服务器 
60. if (client != null) {    
61. "连接到服务器的用户:"
62. try
63. // 第一阶段: 打开输入流 
64. new
65. new
66.   
67. "客户端发送的请求信息: ***************");    
68. // 读取第一行, 请求地址 
69. "http协议头部信息:");    
70.                         String line = in.readLine();   
71.                         System.out.println(line);   
72. '/'),    
73. '/') - 5);    
74. // 获得请求的资源的地址 
75. "gbk");// 反编码 
76.   
77. new
78. // 获取请求方法, GET 或者 POST 
79.   
80. // 读取浏览器发送过来的请求参数头部信息 
81. while ((line = in.readLine()) != null) {    
82.                             System.out.println(line);   
83.   
84. if (line.equals(""))    
85. break;    
86.                         }   
87.   
88. "http协议头部结束 ***************");    
89. "用户请求的资源是:"
90. "请求的类型是: "
91.   
92. null;    
93.   
94. if (resource.indexOf("?") > -1) {    
95.                             params = resource   
96. "?") + 1);    
97. 0, resource    
98. "?"));    
99.                         }   
100.   
101. // 显示 POST 表单提交的内容, 这个内容位于请求的主体部分 
102. if ("POST".equalsIgnoreCase(method)) {    
103. if (params != null) {    
104. "&"
105. else{    
106.                                     params =in.readLine();   
107.                             }   
108.                         }   
109.   
110. "打印提交的数据:");    
111.                         printParams(params);   
112.   
113. // 读取资源并返回给客户端 
114.                         fileReaderAndReturn(resource, client);   
115. // 关闭客户端链接 
116.                         client.close();   
117. "客户端返回完成!");    
118. catch
119. "HTTP服务器错误:"
120.                     }   
121.                 }   
122.   
123. catch
124. "HTTP服务器错误:"
125.             }   
126.         }   
127.     }   
128.   
129. /** 
130.      * 读取一个文件的内容并返回给浏览器端. 
131.      *  
132.      * @param fileName 
133.      *            文件名 
134.      * @param socket 
135.      *            客户端 socket. 
136.      * @throws IOException 
137.      */
138. void fileReaderAndReturn(String fileName, Socket socket) throws
139. if ("/".equals(fileName)) {// 设置欢迎页面,呵呵! 
140. "/index.html";    
141.         }   
142. 1);    
143.   
144. new PrintStream(socket.getOutputStream(), true);    
145. new
146.   
147. ".") + 1);    
148. null;    
149. // 设置返回的内容类型 
150. // 此处的类型与tomcat/conf/web.xml中配置的mime-mapping类型是一致的。测试之用,就写这么几个。 
151. if ("htmlhtmxml".indexOf(fileEx) > -1) {    
152. "text/html;charset=GBK";    
153. else if ("jpegjpggifbpmpng".indexOf(fileEx) > -1) {    
154. "application/binary";    
155.         }   
156.   
157. if
158. // http 协议返回头 
159. "HTTP/1.0 200 OK");// 返回应答消息,并结束应答 
160. "Content-Type:"
161. "Content-Length:" + fileToSend.length());// 返回内容字节数 
162. // 根据 HTTP 协议, 空行将结束头信息 
163.   
164. null;    
165. try
166. new
167. catch
168. "<h1>404错误!</h1>"
169.             }   
170. byte
171. try
172. new byte[fis.available()];    
173.   
174.                 fis.read(data);   
175.                 out.write(data);   
176. catch
177. "<h1>500错误!</h1>"
178.                 e.printStackTrace();   
179. finally
180.                 out.close();   
181. try
182.                     fis.close();   
183. catch
184.   
185.                     e.printStackTrace();   
186.                 }   
187.             }   
188. else
189. "<h1>404错误!</h1>" + "文件没有找到");    
190.             out.close();   
191.   
192.         }   
193.   
194.     }   
195.   
196. void printParams(String params) throws
197. if (params == null) {    
198. return;    
199.         }   
200. "&");    
201. for
202. "=");    
203. 0] + "==" + map[1]);    
204.         }   
205.     }   
206.   
207. /** */
208. /** 
209.      * 启动 HTTP 服务器 
210.      *  
211.      * @param args 
212.      */
213. public static void
214. try
215. if (args.length != 1) {    
216. "这是一个简单的web服务器 ,端口是: 80.");    
217. else if (args.length == 1) {    
218. 0]);    
219.             }   
220. catch
221. "服务器初始化错误"
222.         }   
223.   
224. new
225.     }