public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(80);
//创建ServerSocket对象指定80端口
while (true){
Socket s = ss.accept();
//等待连接
new Thread(()->{
try {
BufferedReader r=new BufferedReader(new InputStreamReader(s.getInputStream()));
String[] split = r.readLine().split(" ");
BufferedInputStream buff = new BufferedInputStream(new FileInputStream(split[1].substring(1)));
//获取浏览器web请求路径
byte[] data=new byte[1024];
int lens;
OutputStream os = s.getOutputStream();
os.write("HTTP/1.1 200 OK\r\n".getBytes());
os.write("Content-Type:text/html\r\n".getBytes());
os.write("\r\n".getBytes());
//返回浏览器响应
while ((lens=buff.read(data))!=-1){
os.write(data,0,lens);
}
os.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaWeb</title>
</head>
<body>
<h1>我的第一个标题</h1>
<p>我的第一个段落。</p>
</body>
</html>