-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleWebServer.java
More file actions
57 lines (50 loc) · 2.18 KB
/
SimpleWebServer.java
File metadata and controls
57 lines (50 loc) · 2.18 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
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.io.*;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Paths;
public class SimpleWebServer {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
// 정적 파일 서빙
server.createContext("/", new StaticFileHandler());
server.createContext("/write", new WriteHandler());
server.setExecutor(null);
server.start();
System.out.println("서버가 http://localhost:8080 에서 실행 중입니다...");
System.out.println("종료하려면 Ctrl+C를 누르세요.");
}
static class StaticFileHandler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String path = exchange.getRequestURI().getPath();
if ("/".equals(path)) {
path = "/index.html";
}
File file = new File("src/main/webapp" + path);
if (file.exists()) {
byte[] content = Files.readAllBytes(file.toPath());
exchange.sendResponseHeaders(200, content.length);
OutputStream os = exchange.getResponseBody();
os.write(content);
os.close();
} else {
String response = "404 Not Found";
exchange.sendResponseHeaders(404, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
static class WriteHandler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String response = "WriteServlet 처리 필요 - 서블릿 컨테이너가 필요합니다.";
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes("UTF-8"));
os.close();
}
}
}