-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.java
More file actions
61 lines (53 loc) · 1.65 KB
/
Copy pathHttpServer.java
File metadata and controls
61 lines (53 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpServer implements Runnable{
public static final int port = 8080; //默认监控端口
public static final String FileRoot = System.getProperty("user.dir"); //默认文件目录
public static void main(String[] args) {
HttpServer server = new HttpServer();
Thread thread = new Thread(server);
thread.start();
}
//多线程
public void run() {
try {
await();
} catch (Exception e) {
System.out.println(e);
}
}
@SuppressWarnings("resource")
public void await() {
ServerSocket serverSocket = null;
try{
serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1")); //监听地址
}
catch (IOException e) {
e.printStackTrace();
}
//
while(true) {
Socket socket = null;
InputStream input = null;
OutputStream output = null;
try {
socket = serverSocket.accept(); //打开一个套接字
input = socket.getInputStream(); //获得套接字输入流
output = socket.getOutputStream(); //获得套接字输出流
Request request = new Request(input);
request.parse();
Response response = new Response(output,request);
response.sendResource();
socket.close();
}
catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}
}