diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f8efdf31..8311dd68d 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,10 +6,11 @@ # 5.8.10.M1 (2022-10-24) ### 🐣新特性 +* 【http 】 HttpResponse增加getFileNameFromDisposition方法(pr#2676@Github) ### 🐞Bug修复 * 【db 】 修复分页时order by截断问题(issue#I5X6FM@Gitee) -* 【db 】 修复Partition计算size除数为0报错问题(pr#2677@Github) +* 【core 】 修复Partition计算size除数为0报错问题(pr#2677@Github) ------------------------------------------------------------------------------------------------------------- # 5.8.9 (2022-10-22) diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java b/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java index a15f87a35..e11be2753 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java @@ -7,6 +7,7 @@ import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.StreamProgress; import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.URLUtil; @@ -448,7 +449,7 @@ public class HttpResponse extends HttpBase implements Closeable { } // 从头信息中获取文件名 - String fileName = getFileNameFromDisposition(); + String fileName = getFileNameFromDisposition(null); if (StrUtil.isBlank(fileName)) { final String path = httpConnection.getUrl().getPath(); // 从路径中获取文件名 @@ -464,6 +465,25 @@ public class HttpResponse extends HttpBase implements Closeable { return FileUtil.file(targetFileOrDir, fileName); } + /** + * 从Content-Disposition头中获取文件名 + * @param paramName 文件参数名 + * + * @return 文件名,empty表示无 + */ + public String getFileNameFromDisposition(String paramName) { + paramName = ObjUtil.defaultIfNull(paramName, "filename"); + String fileName = null; + final String disposition = header(Header.CONTENT_DISPOSITION); + if (StrUtil.isNotBlank(disposition)) { + fileName = ReUtil.get(paramName+"=\"(.*?)\"", disposition, 1); + if (StrUtil.isBlank(fileName)) { + fileName = StrUtil.subAfter(disposition, paramName + "=", true); + } + } + return fileName; + } + // ---------------------------------------------------------------- Private method start /** @@ -620,33 +640,5 @@ public class HttpResponse extends HttpBase implements Closeable { } return copyLength; } - - /** - * 从Content-Disposition头中获取文件名,默认为 filename - * - * @return 文件名,empty表示无 - */ - public String getFileNameFromDisposition() { - return getFileNameFromDisposition("filename"); - } - - /** - * 从Content-Disposition头中获取文件名 - * @param paramName 文件参数名 - * - * @return 文件名,empty表示无 - */ - public String getFileNameFromDisposition(String paramName) { - String fileName = null; - final String disposition = header(Header.CONTENT_DISPOSITION); - if (StrUtil.isNotBlank(disposition)) { - fileName = ReUtil.get(paramName+"=\"(.*?)\"", disposition, 1); - if (StrUtil.isBlank(fileName)) { - fileName = StrUtil.subAfter(disposition, "filename=", true); - } - } - return fileName; - } - // ---------------------------------------------------------------- Private method end }