diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ee25e96c..3c938dfa9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ * 【core 】 增加RadixUtil(pr#260@Gitee) * 【core 】 BeanUtil.getFieldValue支持获取字段集合(pr#254@Gitee) * 【core 】 DateConvert转换失败默认抛出异常(issue#I2M5GN@Gitee) +* 【http 】 HttpServerRequest增加getParam方法 +* 【http 】 RootAction增加可选name参数,返回指定文件名称 ### Bug修复 * 【core 】 修复FileUtil.move以及PathUtil.copy等无法自动创建父目录的问题(issue#I2CKTI@Gitee) diff --git a/hutool-http/src/main/java/cn/hutool/http/server/HttpServerRequest.java b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerRequest.java index 872a3761d..124ae2bfd 100644 --- a/hutool-http/src/main/java/cn/hutool/http/server/HttpServerRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerRequest.java @@ -26,6 +26,7 @@ import java.net.URI; import java.nio.charset.Charset; import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Map; /** @@ -296,6 +297,32 @@ public class HttpServerRequest extends HttpServerBase { return this.httpExchange.getRequestBody(); } + /** + * 获取指定名称的参数值,取第一个值 + * @param name 参数名 + * @return 参数值 + * @since 5.5.8 + */ + public String getParam(String name){ + return getParams().get(name, 0); + } + + /** + * 获取指定名称的参数值 + * + * @param name 参数名 + * @return 参数值 + * @since 5.5.8 + */ + public List getParams(String name){ + return getParams().get(name); + } + + /** + * 获取参数Map + * + * @return 参数map + */ public ListValueMap getParams() { if (null == this.paramsCache) { this.paramsCache = new ListValueMap<>(); 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 5999a9b41..bad6c1ccb 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 @@ -371,11 +371,25 @@ public class HttpServerResponse extends HttpServerBase { * @since 5.2.6 */ public HttpServerResponse write(File file) { + return write(file, null); + } + + /** + * 返回文件给客户端(文件下载) + * + * @param file 写出的文件对象 + * @return this + * @since 5.5.8 + */ + public HttpServerResponse write(File file, String fileName) { final long fileSize = file.length(); if(fileSize > Integer.MAX_VALUE){ throw new IllegalArgumentException("File size is too bigger than " + Integer.MAX_VALUE); } - final String fileName = file.getName(); + + if(StrUtil.isBlank(fileName)){ + fileName = file.getName(); + } final String contentType = ObjectUtil.defaultIfNull(HttpUtil.getMimeType(fileName), "application/octet-stream"); BufferedInputStream in = null; try { 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 25f165756..8c10bd998 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 @@ -64,6 +64,7 @@ public class RootAction implements Action { @Override public void doAction(HttpServerRequest request, HttpServerResponse response) { final String path = request.getPath(); + File file = FileUtil.file(rootDir, path); if (file.exists()) { if (file.isDirectory()) { @@ -75,7 +76,8 @@ public class RootAction implements Action { } } } else{ - response.write(file); + final String name = request.getParam("name"); + response.write(file, name); } }