diff --git a/CHANGELOG.md b/CHANGELOG.md index a3ff4228a..9007a818d 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### 🐣新特性 ### 🐞Bug修复 +* 【http 】 修复getFileNameFromDisposition不符合规范问题(issue#IAKBPD@Gitee) ------------------------------------------------------------------------------------------------------------- # 5.8.31(2024-08-12) 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 de1872d7c..748efc2c1 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java @@ -504,7 +504,6 @@ public class HttpResponse extends HttpBase implements Closeable { } // ---------------------------------------------------------------- Private method start - /** * 从Content-Disposition头中获取文件名 * @@ -513,14 +512,39 @@ public class HttpResponse extends HttpBase implements Closeable { * @return 文件名,empty表示无 */ private static String getFileNameFromDispositions(final List dispositions, String paramName) { + // 正则转义 + paramName = StrUtil.replace(paramName, "*", "\\*"); String fileName = null; - for (String disposition : dispositions) { - fileName = ReUtil.getGroup1(paramName + "=\"(.*?)\"", disposition); + for (final String disposition : dispositions) { + fileName = ReUtil.getGroup1(paramName + "=([^;]+)", disposition); if (StrUtil.isNotBlank(fileName)) { break; } } - return fileName; + return getRfc5987Value(fileName); + } + + /** + * 获取rfc5987标准的值,标准见:https://www.rfc-editor.org/rfc/rfc5987#section-3.2.1
+ * 包括: + * + *
    + *
  • Non-extended:无双引号包裹的值
  • + *
  • Non-extended:双引号包裹的值
  • + *
  • Extended notation:编码'语言'值
  • + *
+ * + * @param value 值 + * @return 结果值 + */ + private static String getRfc5987Value(final String value){ + final List split = StrUtil.split(value, '\''); + if(3 == split.size()){ + return split.get(2); + } + + // 普通值 + return StrUtil.unWrap(value, '"'); } /**