Browse Source

add method

Looly 4 years ago
parent
commit
7525a9a2d1

+ 2 - 0
CHANGELOG.md

@@ -18,6 +18,8 @@
 * 【core   】     增加RadixUtil(pr#260@Gitee)
 * 【core   】     增加RadixUtil(pr#260@Gitee)
 * 【core   】     BeanUtil.getFieldValue支持获取字段集合(pr#254@Gitee)
 * 【core   】     BeanUtil.getFieldValue支持获取字段集合(pr#254@Gitee)
 * 【core   】     DateConvert转换失败默认抛出异常(issue#I2M5GN@Gitee)
 * 【core   】     DateConvert转换失败默认抛出异常(issue#I2M5GN@Gitee)
+* 【http   】     HttpServerRequest增加getParam方法
+* 【http   】     RootAction增加可选name参数,返回指定文件名称
 
 
 ### Bug修复
 ### Bug修复
 * 【core   】     修复FileUtil.move以及PathUtil.copy等无法自动创建父目录的问题(issue#I2CKTI@Gitee)
 * 【core   】     修复FileUtil.move以及PathUtil.copy等无法自动创建父目录的问题(issue#I2CKTI@Gitee)

+ 27 - 0
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.nio.charset.Charset;
 import java.util.Collection;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 import java.util.Map;
 
 
 /**
 /**
@@ -296,6 +297,32 @@ public class HttpServerRequest extends HttpServerBase {
 		return this.httpExchange.getRequestBody();
 		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<String> getParams(String name){
+		return getParams().get(name);
+	}
+
+	/**
+	 * 获取参数Map
+	 *
+	 * @return 参数map
+	 */
 	public ListValueMap<String, String> getParams() {
 	public ListValueMap<String, String> getParams() {
 		if (null == this.paramsCache) {
 		if (null == this.paramsCache) {
 			this.paramsCache = new ListValueMap<>();
 			this.paramsCache = new ListValueMap<>();

+ 15 - 1
hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java

@@ -371,11 +371,25 @@ public class HttpServerResponse extends HttpServerBase {
 	 * @since 5.2.6
 	 * @since 5.2.6
 	 */
 	 */
 	public HttpServerResponse write(File file) {
 	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();
 		final long fileSize = file.length();
 		if(fileSize > Integer.MAX_VALUE){
 		if(fileSize > Integer.MAX_VALUE){
 			throw new IllegalArgumentException("File size is too bigger than " + 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");
 		final String contentType = ObjectUtil.defaultIfNull(HttpUtil.getMimeType(fileName), "application/octet-stream");
 		BufferedInputStream in = null;
 		BufferedInputStream in = null;
 		try {
 		try {

+ 3 - 1
hutool-http/src/main/java/cn/hutool/http/server/action/RootAction.java

@@ -64,6 +64,7 @@ public class RootAction implements Action {
 	@Override
 	@Override
 	public void doAction(HttpServerRequest request, HttpServerResponse response) {
 	public void doAction(HttpServerRequest request, HttpServerResponse response) {
 		final String path = request.getPath();
 		final String path = request.getPath();
+
 		File file = FileUtil.file(rootDir, path);
 		File file = FileUtil.file(rootDir, path);
 		if (file.exists()) {
 		if (file.exists()) {
 			if (file.isDirectory()) {
 			if (file.isDirectory()) {
@@ -75,7 +76,8 @@ public class RootAction implements Action {
 					}
 					}
 				}
 				}
 			} else{
 			} else{
-				response.write(file);
+				final String name = request.getParam("name");
+				response.write(file, name);
 			}
 			}
 		}
 		}