mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix bug
This commit is contained in:
parent
28fde6ddcf
commit
32235d3b7e
@ -85,8 +85,7 @@ public class Console {
|
||||
public static void log(final Throwable t, final String template, final Object... values) {
|
||||
out.println(StrUtil.format(template, values));
|
||||
if (null != t) {
|
||||
//noinspection CallToPrintStackTrace
|
||||
t.printStackTrace();
|
||||
t.printStackTrace(out);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
|
@ -340,6 +340,22 @@ public final class UrlBuilder implements Builder<String> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否path的末尾加 /
|
||||
*
|
||||
* @param withEngTag 是否path的末尾加 /
|
||||
* @return this
|
||||
* @since 5.8.5
|
||||
*/
|
||||
public UrlBuilder setWithEndTag(final boolean withEngTag) {
|
||||
if (null == this.path) {
|
||||
this.path = UrlPath.of();
|
||||
}
|
||||
|
||||
this.path.setWithEndTag(withEngTag);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加路径节点,路径节点中的"/"会被转义为"%2F"
|
||||
*
|
||||
@ -489,7 +505,7 @@ public final class UrlBuilder implements Builder<String> {
|
||||
final StringBuilder fileBuilder = new StringBuilder();
|
||||
|
||||
// path
|
||||
fileBuilder.append(StrUtil.blankToDefault(getPathStr(), StrUtil.SLASH));
|
||||
fileBuilder.append(getPathStr());
|
||||
|
||||
// query
|
||||
final String query = getQueryStr();
|
||||
|
@ -22,6 +22,16 @@ public class UrlPath {
|
||||
private List<String> segments;
|
||||
private boolean withEngTag;
|
||||
|
||||
/**
|
||||
* 构建UrlPath
|
||||
*
|
||||
* @return UrlPath
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static UrlPath of() {
|
||||
return new UrlPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建UrlPath
|
||||
*
|
||||
@ -30,9 +40,7 @@ public class UrlPath {
|
||||
* @return UrlPath
|
||||
*/
|
||||
public static UrlPath of(final CharSequence pathStr, final Charset charset) {
|
||||
final UrlPath urlPath = new UrlPath();
|
||||
urlPath.parse(pathStr, charset);
|
||||
return urlPath;
|
||||
return of().parse(pathStr, charset);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -142,7 +150,8 @@ public class UrlPath {
|
||||
*/
|
||||
public String build(final Charset charset, final boolean encodePercent) {
|
||||
if (CollUtil.isEmpty(this.segments)) {
|
||||
return StrUtil.EMPTY;
|
||||
// 没有节点的path取决于是否末尾追加/,如果不追加返回空串,否则返回/
|
||||
return withEngTag ? StrUtil.SLASH : StrUtil.EMPTY;
|
||||
}
|
||||
|
||||
final char[] safeChars = encodePercent ? null : new char[]{'%'};
|
||||
@ -157,13 +166,16 @@ public class UrlPath {
|
||||
builder.append(CharUtil.SLASH).append(RFC3986.SEGMENT.encode(segment, charset, safeChars));
|
||||
}
|
||||
}
|
||||
|
||||
if (withEngTag) {
|
||||
if (StrUtil.isEmpty(builder)) {
|
||||
// 空白追加是保证以/开头
|
||||
builder.append(CharUtil.SLASH);
|
||||
}else if (withEngTag && false == StrUtil.endWith(builder, CharUtil.SLASH)) {
|
||||
} else if (false == StrUtil.endWith(builder, CharUtil.SLASH)) {
|
||||
// 尾部没有/则追加,否则不追加
|
||||
builder.append(CharUtil.SLASH);
|
||||
}
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
@ -20,6 +20,17 @@ public class UrlBuilderTest {
|
||||
Assert.assertEquals("http://www.hutool.cn/", buildUrl);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildWithoutSlashTest(){
|
||||
// https://github.com/dromara/hutool/issues/2459
|
||||
String buildUrl = UrlBuilder.of().setScheme("http").setHost("192.168.1.1").setPort(8080).setWithEndTag(false).build();
|
||||
Assert.assertEquals("http://192.168.1.1:8080", buildUrl);
|
||||
|
||||
buildUrl = UrlBuilder.of().setScheme("http").setHost("192.168.1.1").setPort(8080).addQuery("url", "http://192.168.1.1/test/1")
|
||||
.setWithEndTag(false).build();
|
||||
Assert.assertEquals("http://192.168.1.1:8080?url=http://192.168.1.1/test/1", buildUrl);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildTest2() {
|
||||
// path中的+不做处理
|
||||
|
Loading…
x
Reference in New Issue
Block a user