add Handler

This commit is contained in:
Looly 2020-04-02 10:21:21 +08:00
parent c7ce3719e8
commit b2201873ea
8 changed files with 201 additions and 44 deletions

View File

@ -3254,10 +3254,10 @@ public class FileUtil {
*
* @param file 文件
* @param out
* @return 目标文件
* @return 写出的流byte数
* @throws IORuntimeException IO异常
*/
public static File writeToStream(File file, OutputStream out) throws IORuntimeException {
public static long writeToStream(File file, OutputStream out) throws IORuntimeException {
return FileReader.create(file).writeToStream(out);
}
@ -3266,10 +3266,11 @@ public class FileUtil {
*
* @param fullFilePath 文件绝对路径
* @param out 输出流
* @return 写出的流byte数
* @throws IORuntimeException IO异常
*/
public static void writeToStream(String fullFilePath, OutputStream out) throws IORuntimeException {
writeToStream(touch(fullFilePath), out);
public static long writeToStream(String fullFilePath, OutputStream out) throws IORuntimeException {
return writeToStream(touch(fullFilePath), out);
}
/**

View File

@ -1,5 +1,12 @@
package cn.hutool.core.io.file;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.LineHandler;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
@ -11,13 +18,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.LineHandler;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
/**
* 文件读取器
*
@ -249,20 +249,15 @@ public class FileReader extends FileWrapper {
* 将文件写入流中
*
* @param out
* @return File
* @return 写出的流byte数
* @throws IORuntimeException IO异常
*/
public File writeToStream(OutputStream out) throws IORuntimeException {
FileInputStream in = null;
try {
in = new FileInputStream(file);
IoUtil.copy(in, out);
public long writeToStream(OutputStream out) throws IORuntimeException {
try (FileInputStream in = new FileInputStream(this.file)){
return IoUtil.copy(in, out);
}catch (IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(in);
}
return this.file;
}
// -------------------------------------------------------------------------- Interface start

View File

@ -783,7 +783,7 @@ public class HttpUtil {
* @return {@link SimpleServer}
* @since 5.2.6
*/
public static SimpleServer createSimpleServer(int port){
public static SimpleServer createServer(int port){
return new SimpleServer(port);
}
// ----------------------------------------------------------------------------------------- Private method start

View File

@ -50,18 +50,51 @@ public class SimpleServer {
}
}
/**
* 增加请求处理规则
*
* @param path 路径
* @param handler 处理器
* @return this
*/
public SimpleServer addHandler(String path, HttpHandler handler) {
this.server.createContext(path, handler);
return this;
}
/**
* 设置自定义线程池
*
* @param executor {@link Executor}
* @return this
*/
public SimpleServer setExecutor(Executor executor) {
this.server.setExecutor(executor);
return this;
}
public SimpleServer start() {
/**
* 获得原始HttpServer对象
*
* @return {@link HttpServer}
*/
public HttpServer getRawServer(){
return this.server;
}
/**
* 获取服务器地址信息
*
* @return {@link InetSocketAddress}
*/
public InetSocketAddress getAddress(){
return this.server.getAddress();
}
/**
* 启动Http服务器启动后会阻塞当前线程
*/
public void start() {
this.server.start();
return this;
}
}

View File

@ -0,0 +1,91 @@
package cn.hutool.http.server.handler;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import com.sun.net.httpserver.HttpExchange;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
/**
* 请求处理器相关工具类
*
* @since 5.2.6
*/
public class HandlerUtil {
/**
* 返回404页面
*
* @param httpExchange HttpExchange
* @param content 要发送的404页面内容
* @throws IOException IO异常
*/
public static void send404(HttpExchange httpExchange, String content) throws IOException {
if (null == httpExchange) {
return;
}
if (null == content) {
content = "404 Not Found !";
}
httpExchange.sendResponseHeaders(HttpStatus.HTTP_NOT_FOUND, 0);
try (OutputStream out = httpExchange.getResponseBody()) {
IoUtil.writeUtf8(out, false, content);
}
}
/**
* 返回文件
*
* @param httpExchange HttpExchange
* @param file 要发送的文件
* @throws IOException IO异常
*/
public static void sendFile(HttpExchange httpExchange, File file) throws IOException {
if (ArrayUtil.hasNull(httpExchange, file)) {
return;
}
addHeader(httpExchange,
Header.CONTENT_TYPE.toString(),
HttpUtil.getMimeType(file.getName(), "text/html"));
httpExchange.sendResponseHeaders(HttpStatus.HTTP_OK, 0);
try (OutputStream out = httpExchange.getResponseBody()) {
FileUtil.writeToStream(file, out);
}
}
/**
* 增加响应头信息
*
* @param httpExchange HttpExchange
* @param header 头名
* @param value 头值
*/
public static void addHeader(HttpExchange httpExchange, String header, String value) {
if (ArrayUtil.hasEmpty(httpExchange, header)) {
return;
}
httpExchange.getResponseHeaders().add(header, value);
}
/**
* 获取响应头信息
*
* @param httpExchange HttpExchange
* @param header 头名
* @return 不存在返回null
*/
public static String getHeader(HttpExchange httpExchange, String header) {
if (ArrayUtil.hasEmpty(httpExchange, header)) {
return null;
}
return httpExchange.getRequestHeaders().getFirst(header);
}
}

View File

@ -0,0 +1,45 @@
package cn.hutool.http.server.handler;
import cn.hutool.core.io.FileUtil;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.File;
import java.io.IOException;
import java.net.URI;
/**
* 默认的处理器通过解析用户传入的path找到网页根目录下对应文件后返回
*
* @author looly
* @since 5.2.6
*/
public class RootHandler implements HttpHandler {
private final String rootDir;
/**
* 构造
*
* @param rootDir 网页根目录
*/
public RootHandler(String rootDir) {
this.rootDir = rootDir;
}
@Override
public void handle(HttpExchange httpExchange) throws IOException {
final URI uri = httpExchange.getRequestURI();
File file = FileUtil.file(rootDir, uri.getPath());
if (file.exists()) {
if (file.isDirectory()) {
//默认读取主页
file = FileUtil.file(file, "index.html");
}
HandlerUtil.sendFile(httpExchange, file);
}
// 文件未找到
HandlerUtil.send404(httpExchange, null);
}
}

View File

@ -0,0 +1,13 @@
package cn.hutool.http.server;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.server.handler.RootHandler;
public class SimpleServerTest {
public static void main(String[] args) {
HttpUtil.createServer(8888)
.addHandler("/", new RootHandler("D:\\test"))
.start();
}
}

View File

@ -1,21 +0,0 @@
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();
}
}