add SimpleHttpServer

This commit is contained in:
Looly 2020-04-01 08:29:33 +08:00
parent bb3b3dd4ae
commit 4a4cdd3744
5 changed files with 108 additions and 0 deletions

View File

@ -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重启报错问题

View File

@ -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
/**

View File

@ -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;
}
}

View File

@ -0,0 +1,7 @@
/**
* Http服务器封装
*
* @author looly
*
*/
package cn.hutool.http.server;

View File

@ -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();
}
}