diff --git a/CHANGELOG.md b/CHANGELOG.md index 180f1db1e..65b329433 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * 【core 】 StrUtil增加subBetweenAll方法,Console增加where和lineNumber方法(issue#812@Github) * 【core 】 TableMap增加getKeys和getValues方法 * 【json 】 JSONObject和JSONArray增加set方法,标识put弃用 +* 【http 】 增加SimpleHttpServer ### Bug修复 * 【extra 】 修复SpringUtil使用devtools重启报错问题 diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java index 99db09344..1d244b7fa 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java @@ -14,6 +14,7 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.URLUtil; +import cn.hutool.http.server.SimpleServer; import java.io.File; import java.io.InputStream; @@ -774,6 +775,17 @@ public class HttpUtil { final ContentType contentType = ContentType.get(body); return (null == contentType) ? null : contentType.toString(); } + + /** + * 创建简易的Http服务器 + * + * @param port 端口 + * @return {@link SimpleServer} + * @since 5.2.6 + */ + public static SimpleServer createSimpleServer(int port){ + return new SimpleServer(port); + } // ----------------------------------------------------------------------------------------- Private method start /** diff --git a/hutool-http/src/main/java/cn/hutool/http/server/SimpleServer.java b/hutool-http/src/main/java/cn/hutool/http/server/SimpleServer.java new file mode 100644 index 000000000..f1fcc1f6e --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/server/SimpleServer.java @@ -0,0 +1,67 @@ +package cn.hutool.http.server; + +import cn.hutool.core.io.IORuntimeException; +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpServer; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.concurrent.Executor; + +/** + * 简易Http服务器,基于{@link HttpServer} + * + * @author looly + * @since 5.2.5 + */ +public class SimpleServer { + + HttpServer server; + + /** + * 构造 + * + * @param port 监听端口 + */ + public SimpleServer(int port) { + this(new InetSocketAddress(port)); + } + + /** + * 构造 + * + * @param hostname 监听地址 + * @param port 监听端口 + */ + public SimpleServer(String hostname, int port) { + this(new InetSocketAddress(hostname, port)); + } + + /** + * 构造 + * + * @param address 监听地址 + */ + public SimpleServer(InetSocketAddress address) { + try { + this.server = HttpServer.create(address, 0); + } catch (IOException e) { + throw new IORuntimeException(e); + } + } + + public SimpleServer addHandler(String path, HttpHandler handler) { + this.server.createContext(path, handler); + return this; + } + + public SimpleServer setExecutor(Executor executor) { + this.server.setExecutor(executor); + return this; + } + + public SimpleServer start() { + this.server.start(); + return this; + } +} diff --git a/hutool-http/src/main/java/cn/hutool/http/server/package-info.java b/hutool-http/src/main/java/cn/hutool/http/server/package-info.java new file mode 100644 index 000000000..0b65d48e7 --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/server/package-info.java @@ -0,0 +1,7 @@ +/** + * Http服务器封装 + * + * @author looly + * + */ +package cn.hutool.http.server; \ No newline at end of file diff --git a/hutool-http/src/test/java/cn/hutool/http/test/SimpleServerTest.java b/hutool-http/src/test/java/cn/hutool/http/test/SimpleServerTest.java new file mode 100644 index 000000000..43d83973f --- /dev/null +++ b/hutool-http/src/test/java/cn/hutool/http/test/SimpleServerTest.java @@ -0,0 +1,21 @@ +package cn.hutool.http.test; + +import cn.hutool.core.io.IoUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.http.HttpStatus; +import cn.hutool.http.server.SimpleServer; + +import java.io.OutputStream; + +public class SimpleServerTest { + + public static void main(String[] args) { + final SimpleServer server = new SimpleServer(8888); + server.addHandler("/", httpExchange -> { + httpExchange.sendResponseHeaders(HttpStatus.HTTP_OK, 0); + final OutputStream out = httpExchange.getResponseBody(); + out.write(StrUtil.bytes("Hello Hutool Server!")); + IoUtil.close(out); + }).start(); + } +}