fix location bug

This commit is contained in:
Looly
2025-08-18 19:17:33 +08:00
parent 831fed4bb3
commit 10d660fb06

View File

@@ -17,6 +17,7 @@
package cn.hutool.v7.http; package cn.hutool.v7.http;
import cn.hutool.v7.core.net.url.UrlBuilder; import cn.hutool.v7.core.net.url.UrlBuilder;
import cn.hutool.v7.core.text.CharUtil;
import cn.hutool.v7.core.text.StrUtil; import cn.hutool.v7.core.text.StrUtil;
import cn.hutool.v7.core.text.split.SplitUtil; import cn.hutool.v7.core.text.split.SplitUtil;
@@ -42,8 +43,11 @@ public class HttpUrlUtil {
if (!HttpUtil.isHttp(location) && !HttpUtil.isHttps(location)) { if (!HttpUtil.isHttp(location) && !HttpUtil.isHttps(location)) {
// issue#I5TPSY // issue#I5TPSY
// location可能为相对路径 // location可能为相对路径
if (!location.startsWith("/")) { if (!location.startsWith(StrUtil.SLASH)) {
location = StrUtil.addSuffixIfNot(parentUrl.getPathStr(), "/") + location; // issue#ICSG7D 如果当前路径以/结尾,直接拼接,否则去除最后一个节点
String pathStr = parentUrl.getPathStr();
pathStr = StrUtil.subBefore(pathStr, CharUtil.SLASH, true);
location = pathStr + StrUtil.SLASH + location;
} }
// issue#3265, 相对路径中可能存在参数,单独处理参数 // issue#3265, 相对路径中可能存在参数,单独处理参数