String url="/acct/StatsClientListService?clientType=AllClient";
List<String> urlQueryParams =new ArrayList<String>();
String clientType =null;
String urlBool = null;
List<String> urlSplit = Pattern.compile("\\?").splitAsStream(url).collect(Collectors.toList());
//condition based results
if(urlSplit.size() == 2){
urlQueryParams=Pattern.compile("&").splitAsStream(urlSplit.get(1)).collect(Collectors.toList());
if(urlQueryParams.size() == 2 ){
clientType = urlQueryParams.get(0).split("=")[1];
urlBool = urlQueryParams.get(0).split("=")[1];
System.out.println("clientType -- "+clientType + " urlBool ---- "+urlBool);
}
else{
clientType = urlQueryParams.get(0).split("=")[1];
System.out.println("clientType -- "+clientType);
}
}
else{
System.out.println("url without params");
}
答案 0 :(得分:0)
以下代码段可能是一个重点。
URL url = new URL("http://example.com/acct/StatsClientListService"
+ "?clientType=AllClient&something=interesting");
Stream.of(url.getQuery().split("&"))
.collect(Collectors.toMap(
s -> s.replaceFirst("^(.*)=.*", "$1"),
s -> s.replaceFirst("^.*=(.*)", "$1")))
.forEach((k, v) ->
System.out.printf("param: %s value: %s%n", k, v)
);
<强>输出强>
param: clientType value: AllClient
param: something value: interesting
编辑如果网址字符串仅包含路径,并且(可选)您可以从此代码段开始查询。
String urlString = "/acct/StatsClientListService"
+ "?clientType=AllClient&something=interesting";
if (urlString.contains("?")) {
String query = urlString.substring(urlString.indexOf("?") + 1);
Stream.of(query.split("&"))
.collect(Collectors.toMap(
s -> s.replaceFirst("^(.*)=.*", "$1"),
s -> s.replaceFirst("^.*=(.*)", "$1")))
.forEach((k, v)
-> System.out.printf("param: %s value: %s%n", k, v)
);
}
<强>输出强>
param: clientType value: AllClient
param: something value: interesting
注意有必要对urlString
进行额外检查。取决于可能的值。