📅  最后修改于: 2022-03-11 14:52:25.313000             🧑  作者: Mango
public class BasicHttpServerExample {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8500), 0);
HttpContext context = server.createContext("/");
context.setHandler(BasicHttpServerExample::handleRequest);
server.start();
}
private static void handleRequest(HttpExchange exchange) throws IOException {
String response = "Hi there!";
exchange.sendResponseHeaders(200, response.getBytes().length);//response code and length
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}