mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix simple server
This commit is contained in:
@@ -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/");
|
||||
}
|
||||
}
|
||||
|
@@ -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) {
|
||||
|
14
hutool-http/src/test/resources/html/formForUpload.html
Normal file
14
hutool-http/src/test/resources/html/formForUpload.html
Normal 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>
|
7
hutool-http/src/test/resources/html/index.html
Normal file
7
hutool-http/src/test/resources/html/index.html
Normal file
@@ -0,0 +1,7 @@
|
||||
<!DOCTYPE html>
|
||||
<html class="no-js">
|
||||
<body>
|
||||
<h1>Hutool</h1>
|
||||
<h3>Simple Server of Hutool</h3>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user