Java 从 url 字符串中摆脱获取参数的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27267111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:03:56  来源:igfitidea点击:

What's the best way to get rid of get parameters from url string?

java

提问by Maksym

I have URL string like:

我有如下 URL 字符串:

"http://www.xyz/path1/path2/path3?param1=value1&param2=value2".

http://www.xyz/path1/path2/path3?param1=value1¶m2=value2”。

I need to get this url without parameters, so the result should be:

我需要在没有参数的情况下获取这个 url,所以结果应该是:

"http://www.xyz/path1/path2/path3".

http://www.xyz/path1/path2/path3”。

I have done it this way:

我是这样做的:

private String getUrlWithoutParameters(String url)
{
  return url.substring(0,url.lastIndexOf('?'));
}

Are there any better ways to do it?

有没有更好的方法来做到这一点?

采纳答案by Eran

Probably not the most efficient way, but more type safe :

可能不是最有效的方法,但更安全:

private String getUrlWithoutParameters(String url) throws URISyntaxException {
    URI uri = new URI(url);
    return new URI(uri.getScheme(),
                   uri.getAuthority(),
                   uri.getPath(),
                   null, // Ignore the query part of the input url
                   uri.getFragment()).toString();
}

回答by PbxMan

I normally use

我通常使用

url.split("\?")[0]

回答by SMA

Try using substring and indexOf method in String:

尝试在 String 中使用 substring 和 indexOf 方法:

String str = "http://www.xyz/path1/path2/path3?param1=value1&param2=value2";
int index = str.indexOf("?");
if (index != -1) {
    System.out.println(str.substring(0, str.indexOf("?")));
} else {
    System.out.println("You dont have question mark in your url");
}

回答by Joop Eggen

With an URL it could be done by methods. With a String:

使用 URL 可以通过方法来完成。使用字符串:

url = url.replaceFirst("\?.*$", "");

This attempts to replace all starting with a question mark. When no question mark, the original string is kept.

这试图替换所有以问号开头的内容。当没有问号时,保留原始字符串。

回答by dchar

You could use something like the following, that removes the query part from the URL.

您可以使用类似以下内容,从 URL 中删除查询部分。

private String getUrlWithoutParameters(String url) throws MalformedURLException {
    return url.replace(new URL(url).getQuery(), "");
}

Alternatively, you might want to check if url rewrite covers your needs: http://tuckey.org/urlrewrite/

或者,您可能想检查 url rewrite 是否满足您的需求:http: //tuckey.org/urlrewrite/

回答by Adrian Baker

Using javax.ws.rs.core.UriBuilderfrom JAX-RS 2.0:

使用JAX-RS 2.0 中的javax.ws.rs.core.UriBuilder

UriBuilder.fromUri("https://www.google.co.nz/search?q=test").replaceQuery(null).build();

Using the very similar org.springframework.web.util.UriBuilderfrom Spring:

使用Spring 中非常相似的org.springframework.web.util.UriBuilder

UriComponentsBuilder.fromUriString("https://www.google.co.nz/search?q=test").replaceQuery(null).build(Collections.emptyMap());

回答by sendon1982

You can use org.apache.http.client.utils.URIBuilderfrom httpclient jar

您可以org.apache.http.client.utils.URIBuilder从 httpclient jar 中使用

@Test
void test_removeQueryParameters() throws Exception {
    String url = "http://www.google.com?query=Shanghai&foo=bar";
    String expectedUrl = "http://www.google.com";

    String result = removeQueryParameters(url);
    Assert.assertThat(result, equalTo(expectedUrl));
}

public String removeQueryParameters(String url) throws URISyntaxException {
    URIBuilder uriBuilder = new URIBuilder(url);
    uriBuilder.removeQuery();

    return uriBuilder.build().toString();
}