mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add SimpleHttpServer
This commit is contained in:
parent
bb3b3dd4ae
commit
4a4cdd3744
@ -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重启报错问题
|
||||
|
@ -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
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Http服务器封装
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.http.server;
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user