Browse Source

fix simple server

Looly 5 years ago
parent
commit
76d6768790

+ 1 - 0
CHANGELOG.md

@@ -19,6 +19,7 @@
 * 【core  】     修复BeanUtil.mapToBean中bean的class非空构造无法实例化问题
 * 【core  】     修复NamedSql多个连续变量出现替换问题
 * 【core  】     修复Bean重名字段(大小写区别)获取数据出错的问题(issue#I1QBQ4@Gitee)
+* 【http  】     修复SimpleServer响应头无效问题(issue#1006@Github)
 
 -------------------------------------------------------------------------------------------------------------
 

+ 7 - 8
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);
 	}
 }

+ 11 - 0
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));
 	}
 

+ 23 - 1
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<String> 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 !");
 	}
 }

+ 5 - 3
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/");
 	}
 }

+ 7 - 3
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) {

+ 14 - 0
hutool-http/src/test/resources/html/formForUpload.html

@@ -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 - 0
hutool-http/src/test/resources/html/index.html

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