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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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));
|
||||
}
|
||||
|
||||
|
@@ -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 !");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user