fix simple server

This commit is contained in:
Looly
2020-08-07 15:51:31 +08:00
parent 7b0e6dddc0
commit 76d6768790
8 changed files with 75 additions and 15 deletions

View File

@@ -1,13 +1,15 @@
package cn.hutool.http.server;
import cn.hutool.core.swing.DesktopUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpUtil;
public class BlankServerTest {
public static void main(String[] args) {
HttpUtil.createServer(8888)
.addAction("/", (req, res)->{
res.write("Hello Hutool Server");
})
.addAction("/", (req, res)-> res.write("Hello Hutool Server", ContentType.JSON.getValue()))
.start();
DesktopUtil.browse("http://localhost:8888/");
}
}

View File

@@ -1,5 +1,6 @@
package cn.hutool.http.server;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.net.multipart.UploadFile;
import cn.hutool.http.ContentType;
@@ -9,8 +10,8 @@ public class SimpleServerTest {
public static void main(String[] args) {
HttpUtil.createServer(8888)
// 设置默认根目录,
.setRoot("d:/test")
// 设置默认根目录,classpath/html
.setRoot(FileUtil.file("html"))
// get数据测试返回请求的PATH
.addAction("/get", (request, response) ->
response.write(request.getURI().toString(), ContentType.TEXT_PLAIN.toString())
@@ -24,9 +25,12 @@ 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
// http://localhost:8888/formForUpload.html
.addAction("/file", (request, response) -> {
Console.log("Upload file...");
Console.log(request.getParams());
final UploadFile[] files = request.getMultipart().getFiles("file");
// 传入目录默认读取HTTP头中的文件名然后创建文件
for (UploadFile file : files) {

View File

@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>文件上传表单提交</title>
</head>
<body>
<h1>文件上传测试</h1>
<form action="file" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file"><br>
<input type="submit" />
</form>
</body>
</html>

View File

@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html class="no-js">
<body>
<h1>Hutool</h1>
<h3>Simple Server of Hutool</h3>
</body>
</html>