返回顶部

收藏

MINA 实现的简易 HTTP 服务器

更多

[Java]代码

public class Test {
    public static void main(String[] args) throws IOException {
        HttpServer httpServer;
        httpServer = HttpServer.create(new InetSocketAddress(81), 5);
        httpServer.createContext("/", new Handler());
        httpServer.start();
    }

    static class Handler implements HttpHandler {
        public void handle(HttpExchange exchange) throws IOException {
            Headers requestHeaders = exchange.getRequestHeaders();
            Headers responseHeaders = exchange.getResponseHeaders();
            responseHeaders.set("Content-Type", "text/plain");
            exchange.sendResponseHeaders(200, 0L);
            OutputStream responseBody = new BufferedOutputStream(exchange.getResponseBody(), 64*1024);
            responseBody.write("Hello!".getBytes());
            responseBody.close();
            exchange.close();
        }
    }
}

标签:MINA,HTTP

收藏

0人收藏

支持

0

反对

0

发表评论