From 76d6768790e6ba64c6c7994d8a67908269aaab58 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 7 Aug 2020 15:51:31 +0800 Subject: [PATCH] fix simple server --- CHANGELOG.md | 1 + .../http/server/HttpServerResponse.java | 15 ++++++------ .../cn/hutool/http/server/SimpleServer.java | 11 +++++++++ .../hutool/http/server/action/RootAction.java | 24 ++++++++++++++++++- .../hutool/http/server/BlankServerTest.java | 8 ++++--- .../hutool/http/server/SimpleServerTest.java | 10 +++++--- .../test/resources/html/formForUpload.html | 14 +++++++++++ .../src/test/resources/html/index.html | 7 ++++++ 8 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 hutool-http/src/test/resources/html/formForUpload.html create mode 100644 hutool-http/src/test/resources/html/index.html diff --git a/CHANGELOG.md b/CHANGELOG.md index d8b85edf4..960b31e84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ * 【core 】 修复BeanUtil.mapToBean中bean的class非空构造无法实例化问题 * 【core 】 修复NamedSql多个连续变量出现替换问题 * 【core 】 修复Bean重名字段(大小写区别)获取数据出错的问题(issue#I1QBQ4@Gitee) +* 【http 】 修复SimpleServer响应头无效问题(issue#1006@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java index 2b82b144d..848250d15 100644 --- a/hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java +++ b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java @@ -10,7 +10,6 @@ import cn.hutool.http.ContentType; import cn.hutool.http.Header; import cn.hutool.http.HttpStatus; import cn.hutool.http.HttpUtil; - import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; @@ -116,9 +115,6 @@ public class HttpServerResponse extends HttpServerBase { * @return 响应头 */ public Headers getHeaders() { - if (false == this.isSendCode) { - sendOk(); - } return this.httpExchange.getResponseHeaders(); } @@ -356,10 +352,13 @@ public class HttpServerResponse extends HttpServerBase { * @param fileName 文件名 * @since 5.2.6 */ - public void write(InputStream in, String contentType, String fileName) { + public void write(InputStream in, String contentType, String fileName) { final Charset charset = ObjectUtil.defaultIfNull(this.charset, DEFAULT_CHARSET); - setHeader("Content-Disposition", StrUtil.format("attachment;filename={}", URLUtil.encode(fileName, charset))); - setContentType(contentType); - write(in); + + if(false == contentType.startsWith("text/")){ + // 非文本类型数据直接走下载 + setHeader(Header.CONTENT_DISPOSITION, StrUtil.format("attachment;filename={}", URLUtil.encode(fileName, charset))); + } + write(in, contentType); } } diff --git a/hutool-http/src/main/java/cn/hutool/http/server/SimpleServer.java b/hutool-http/src/main/java/cn/hutool/http/server/SimpleServer.java index aa1377963..f62141613 100644 --- a/hutool-http/src/main/java/cn/hutool/http/server/SimpleServer.java +++ b/hutool-http/src/main/java/cn/hutool/http/server/SimpleServer.java @@ -8,6 +8,7 @@ import cn.hutool.http.server.handler.ActionHandler; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; +import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; import java.util.concurrent.Executor; @@ -73,6 +74,16 @@ public class SimpleServer { * @return this */ public SimpleServer setRoot(String root) { + return setRoot(new File(root)); + } + + /** + * 设置根目录,默认的页面从root目录中读取解析返回 + * + * @param root 路径 + * @return this + */ + public SimpleServer setRoot(File root) { return addAction("/", new RootAction(root)); } diff --git a/hutool-http/src/main/java/cn/hutool/http/server/action/RootAction.java b/hutool-http/src/main/java/cn/hutool/http/server/action/RootAction.java index 9b4c66803..6b0ea3c64 100644 --- a/hutool-http/src/main/java/cn/hutool/http/server/action/RootAction.java +++ b/hutool-http/src/main/java/cn/hutool/http/server/action/RootAction.java @@ -2,6 +2,7 @@ package cn.hutool.http.server.action; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.lang.Console; import cn.hutool.http.server.HttpServerRequest; import cn.hutool.http.server.HttpServerResponse; @@ -18,7 +19,7 @@ public class RootAction implements Action { public static final String DEFAULT_INDEX_FILE_NAME = "index.html"; - private final String rootDir; + private final File rootDir; private final List indexFileNames; /** @@ -27,6 +28,15 @@ public class RootAction implements Action { * @param rootDir 网页根目录 */ public RootAction(String rootDir) { + this(new File(rootDir)); + } + + /** + * 构造 + * + * @param rootDir 网页根目录 + */ + public RootAction(File rootDir) { this(rootDir, DEFAULT_INDEX_FILE_NAME); } @@ -37,6 +47,17 @@ public class RootAction implements Action { * @param indexFileNames 主页文件名列表 */ public RootAction(String rootDir, String... indexFileNames) { + this(new File(rootDir), indexFileNames); + } + + /** + * 构造 + * + * @param rootDir 网页根目录 + * @param indexFileNames 主页文件名列表 + * @since 5.4.0 + */ + public RootAction(File rootDir, String... indexFileNames) { this.rootDir = rootDir; this.indexFileNames = CollUtil.toList(indexFileNames); } @@ -59,6 +80,7 @@ public class RootAction implements Action { } } + Console.log(file.getAbsolutePath()); response.send404("404 Not Found !"); } } diff --git a/hutool-http/src/test/java/cn/hutool/http/server/BlankServerTest.java b/hutool-http/src/test/java/cn/hutool/http/server/BlankServerTest.java index b5819778e..d146ddcac 100644 --- a/hutool-http/src/test/java/cn/hutool/http/server/BlankServerTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/server/BlankServerTest.java @@ -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/"); } } diff --git a/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java b/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java index f76021b18..633943dc7 100644 --- a/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java @@ -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) { diff --git a/hutool-http/src/test/resources/html/formForUpload.html b/hutool-http/src/test/resources/html/formForUpload.html new file mode 100644 index 000000000..ec786be71 --- /dev/null +++ b/hutool-http/src/test/resources/html/formForUpload.html @@ -0,0 +1,14 @@ + + + + + 文件上传表单提交 + + +

文件上传测试

+
+ 文件:
+ +
+ + \ No newline at end of file diff --git a/hutool-http/src/test/resources/html/index.html b/hutool-http/src/test/resources/html/index.html new file mode 100644 index 000000000..5531dcc49 --- /dev/null +++ b/hutool-http/src/test/resources/html/index.html @@ -0,0 +1,7 @@ + + + +

Hutool

+

Simple Server of Hutool

+ + \ No newline at end of file