This commit is contained in:
Looly
2020-04-10 17:16:21 +08:00
parent 942521862d
commit a967dd6c79
4 changed files with 25 additions and 11 deletions

View File

@@ -3,6 +3,8 @@ package cn.hutool.http.server.action;
import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import java.io.IOException;
/**
* 请求处理接口<br>
* 当用户请求某个Path则调用相应Action的doAction方法
@@ -18,5 +20,5 @@ public interface Action {
* @param request 请求对象
* @param response 响应对象
*/
void doAction(HttpServerRequest request, HttpServerResponse response);
void doAction(HttpServerRequest request, HttpServerResponse response) throws IOException;
}

View File

@@ -6,6 +6,8 @@ import cn.hutool.http.server.action.Action;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
/**
* Action处理器用于将HttpHandler转换为Action形式
*
@@ -26,7 +28,7 @@ public class ActionHandler implements HttpHandler {
}
@Override
public void handle(HttpExchange httpExchange) {
public void handle(HttpExchange httpExchange) throws IOException {
action.doAction(
new HttpServerRequest(httpExchange),
new HttpServerResponse(httpExchange)

View File

@@ -1,5 +1,7 @@
package cn.hutool.http.server;
import cn.hutool.core.lang.Console;
import cn.hutool.core.net.multipart.UploadFile;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpUtil;
@@ -18,6 +20,16 @@ public class SimpleServerTest {
.addAction("/formTest", (request, response) ->
response.write(request.getParams().toString(), ContentType.TEXT_PLAIN.toString())
)
// 文件上传测试
// http://localhost:8888/formTest?a=1&a=2&b=3
.addAction("/file", (request, response) -> {
final UploadFile file = request.getMultipart().getFile("file");
// 传入目录默认读取HTTP头中的文件名然后创建文件
file.write("d:/test/");
Console.log("Write file to: d:/test/");
response.write(request.getParams().toString(), ContentType.TEXT_PLAIN.toString());
}
)
.start();
}
}