【问题标题】:Parse a URI String into Name-Value Collection将 URI 字符串解析为名称-值集合
【发布时间】:2012-11-15 13:06:51
【问题描述】:

我有这样的 URI:

https://google.com.ua/oauth/authorize?client_id=SS&response_type=code&scope=N_FULL&access_type=offline&redirect_uri=http://localhost/Callback

我需要一个包含已解析元素的集合:

NAME               VALUE
------------------------
client_id          SS
response_type      code
scope              N_FULL
access_type        offline
redirect_uri       http://localhost/Callback

确切地说,我需要 C#/.NET HttpUtility.ParseQueryString 方法的 Java 等效项。

【问题讨论】:

标签: java parsing uri namevaluecollection


【解决方案1】:

如果您正在寻找一种不使用外部库来实现它的方法,以下代码将为您提供帮助。

public static Map<String, String> splitQuery(URL url) throws UnsupportedEncodingException {
    Map<String, String> query_pairs = new LinkedHashMap<String, String>();
    String query = url.getQuery();
    String[] pairs = query.split("&");
    for (String pair : pairs) {
        int idx = pair.indexOf("=");
        query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
    }
    return query_pairs;
}

您可以使用&lt;map&gt;.get("client_id") 访问返回的地图,使用您问题中给出的 URL,这将返回“SS”。

更新添加了 URL 解码

更新由于这个答案仍然很受欢迎,我对上面的方法做了一个改进版本,它可以处理具有相同键的多个参数和没有值的参数。

public static Map<String, List<String>> splitQuery(URL url) throws UnsupportedEncodingException {
  final Map<String, List<String>> query_pairs = new LinkedHashMap<String, List<String>>();
  final String[] pairs = url.getQuery().split("&");
  for (String pair : pairs) {
    final int idx = pair.indexOf("=");
    final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
    if (!query_pairs.containsKey(key)) {
      query_pairs.put(key, new LinkedList<String>());
    }
    final String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8") : null;
    query_pairs.get(key).add(value);
  }
  return query_pairs;
}

更新Java8版本

public Map<String, List<String>> splitQuery(URL url) {
    if (Strings.isNullOrEmpty(url.getQuery())) {
        return Collections.emptyMap();
    }
    return Arrays.stream(url.getQuery().split("&"))
            .map(this::splitQueryParameter)
            .collect(Collectors.groupingBy(SimpleImmutableEntry::getKey, LinkedHashMap::new, mapping(Map.Entry::getValue, toList())));
}

public SimpleImmutableEntry<String, String> splitQueryParameter(String it) {
    final int idx = it.indexOf("=");
    final String key = idx > 0 ? it.substring(0, idx) : it;
    final String value = idx > 0 && it.length() > idx + 1 ? it.substring(idx + 1) : null;
    return new SimpleImmutableEntry<>(
        URLDecoder.decode(key, "UTF-8"),
        URLDecoder.decode(value, "UTF-8")
    );
}

使用 URL 运行上述方法

https://stackoverflow.com?param1=value1&amp;param2=&amp;param3=value3&amp;param3

返回此地图:

{param1=["value1"], param2=[null], param3=["value3", null]}

【讨论】:

  • 您忘记了对名称和参数进行解码,这也是让库执行常见任务通常更好的原因之一。
  • 我的回答指出了一个常用的库(apache),如果你还没有使用它,它可能不值得。答案现在看起来不错,并提供了 OP 想要的地图
  • 如果您有多个具有相同名称/键的参数,使用此函数将覆盖具有相似键的值。
  • @Chris 您将 xml/html 转义与 URL 编码混淆了。您的示例网址应为:a.com/q?1=a%26b&2=b%26c
  • 最好指出使用了哪些函数:Collectors.mapping(...) 和 Collectors.toList(...)
【解决方案2】:

org.apache.http.client.utils.URLEncodedUtils

是一个可以为你做这件事的知名图书馆

import org.apache.hc.client5.http.utils.URLEncodedUtils

String url = "http://www.example.com/something.html?one=1&two=2&three=3&three=3a";

List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), Charset.forName("UTF-8"));

for (NameValuePair param : params) {
  System.out.println(param.getName() + " : " + param.getValue());
}

输出

one : 1
two : 2
three : 3
three : 3a

【讨论】:

  • 我可以在不传递所有元素的情况下通过其名称接收值吗?我的意思是这样的: System.out.print(params["one"]);
  • @SergeyShafiev 将 List&lt;NameValuePair&gt; 转换为 Map&lt;String,String&gt; 很简单 Java 没有哈希映射的括号访问权限,它看起来像 map.get("one") 如果你不知道如何这样做,这应该是另一个问题(但首先要自己尝试)。我们更愿意在 SO 中保持问题的规模
  • 请注意,如果您的 URL 中有两次相同的参数(即 ?a=1&a=2),URLEncodedUtils 将抛出 IllegalArgumentException
  • @Crystark 从 httpclient 4.3.3 开始,具有重复名称的查询字符串不会引发任何异常。它按预期工作。 System.out.println(URLEncodedUtils.parse(new URI("http://example.com/?foo=bar&amp;foo=baz"), "UTF-8")); 将打印 [foo=bar, foo=baz]
  • 从 Android 6 开始,Apache HTTP 客户端库已被移除。这意味着 URLEncodedUtils and NameValuePair` 不再可用(除非您按照 here 的描述向旧 Apache 库添加依赖项)。
【解决方案3】:

如果您使用的是 Spring 框架:

public static void main(String[] args) {
    String uri = "http://my.test.com/test?param1=ab&param2=cd&param2=ef";
    MultiValueMap<String, String> parameters =
            UriComponentsBuilder.fromUriString(uri).build().getQueryParams();
    List<String> param1 = parameters.get("param1");
    List<String> param2 = parameters.get("param2");
    System.out.println("param1: " + param1.get(0));
    System.out.println("param2: " + param2.get(0) + "," + param2.get(1));
}

你会得到:

param1: ab
param2: cd,ef

【讨论】:

  • 对于 URL 使用 UriComponentsBuilder.fromHttpUrl(url)
  • 注意getQueryParams() 确实解码查询参数。因此,对于 http://foobar/path?param1=a%3Db 的 URL,您会得到 param1: a%3Db 而不是 param1: a=b。您需要自己使用URLDecoder.decode()... - getQueryParams() BROKEN
【解决方案4】:

使用 google Guava 并分两行完成:

import java.util.Map;
import com.google.common.base.Splitter;

public class Parser {
    public static void main(String... args) {
        String uri = "https://google.com.ua/oauth/authorize?client_id=SS&response_type=code&scope=N_FULL&access_type=offline&redirect_uri=http://localhost/Callback";
        String query = uri.split("\\?")[1];
        final Map<String, String> map = Splitter.on('&').trimResults().withKeyValueSeparator('=').split(query);
        System.out.println(map);
    }
}

给你

{client_id=SS, response_type=code, scope=N_FULL, access_type=offline, redirect_uri=http://localhost/Callback}

【讨论】:

  • 所选答案中描述的URL解码呢?
  • 这对同名的多个键也很可疑。根据 javadocs,这将引发 IllegalArgumentException
  • 您应该使用new java.net.URL(uri).getQuery(),而不是手动拆分uri,因为这样可以为您购买免费的URL输入验证。
  • 用于解码:最终 Map queryVars = Maps.transformValues(map, new Function() { @Override public String apply(String value) { try { return URLDecoder .decode(value, "UTF-8"); } catch (UnsupportedEncodingException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return value; } });
  • 警告!!这样做是不安全的,因为如果查询字符串中有重复的键,splitter.split() 将抛出 IllegalArgumentException。见stackoverflow.com/questions/1746507/…
【解决方案5】:

我找到的最短的方法是这个:

MultiValueMap<String, String> queryParams =
            UriComponentsBuilder.fromUriString(url).build().getQueryParams();

更新: UriComponentsBuilder 来自 Spring。这里the link

【讨论】:

  • 不知道这个 UriComponentsBuilder 类是从哪里来的,它不是很有用。
  • 注意 这需要 URI。 Java 版本的 URI 不是 URL 的超集(这就是 toURI 可以抛出异常的原因)。
  • 注意getQueryParams() 确实解码查询参数。因此,对于http://foobar/path?param1=a%3Db 的 URL,您会得到 param1: a%3Db 而不是 param1: a=b。你需要自己使用URLDecoder.decode()... - getQueryParams() BROKEN
【解决方案6】:

对于 Android,如果您在项目中使用 OkHttp。你可以看看这个。它简单而有用。

final HttpUrl url = HttpUrl.parse(query);
if (url != null) {
    final String target = url.queryParameter("target");
    final String id = url.queryParameter("id");
}

【讨论】:

  • HttpUrl 是一个奇怪的名字,但这正是我所需要的。谢谢。
  • 更新:HttpUrl.parse() 自 OkHttp 4 起已弃用,但使用此处描述的新 OkHttp 扩展功能仍然可以做到这一点:stackoverflow.com/a/63118203/2888763
【解决方案7】:

纯 Java 11

给定要分析的 URL:

URL url = new URL("https://google.com.ua/oauth/authorize?client_id=SS&response_type=code&scope=N_FULL&access_type=offline&redirect_uri=http://localhost/Callback");

此解决方案收集对的列表:

List<Map.Entry<String, String>> list = Pattern.compile("&")
   .splitAsStream(url.getQuery())
   .map(s -> Arrays.copyOf(s.split("=", 2), 2))
   .map(o -> Map.entry(decode(o[0]), decode(o[1])))
   .collect(Collectors.toList());

另一方面,此解决方案收集地图(假设在 url 中可以有更多具有相同名称但值不同的参数)。

Map<String, List<String>> list = Pattern.compile("&")
   .splitAsStream(url.getQuery())
   .map(s -> Arrays.copyOf(s.split("=", 2), 2))
   .collect(groupingBy(s -> decode(s[0]), mapping(s -> decode(s[1]), toList())));

两种解决方案都必须使用实用函数来正确解码参数。

private static String decode(final String encoded) {
    return Optional.ofNullable(encoded)
                   .map(e -> URLDecoder.decode(e, StandardCharsets.UTF_8))
                   .orElse(null);
}

【讨论】:

  • 这更像是一种 Java 8 方法,而不是 Java 8 oneliner。
  • IMO,oneliner 应该很短并且不应该跨越多行。
  • 这里涉及到多个语句。
  • 我猜你可以在一行上写一个完整的类,但这不是通常所说的“单行”这个词的意思。
  • 如果您碰巧拥有 Java 10 或更高版本,则稍有改进 - URLDecoder#decode(最终)有一个重载,它采用字符集(例如 StandardCharsets.UTF_8)而不是字符串进行编码,这意味着您不需要捕获 UnsupportedEncodingException。
【解决方案8】:

如果你正在使用 servlet doGet 试试这个

request.getParameterMap()

返回此请求参数的 java.util.Map。

返回: 一个不可变的 java.util.Map 包含作为键的参数名称和作为映射值的参数值。参数映射中的键是字符串类型。参数映射中的值是字符串数组类型。

(Java doc)

【讨论】:

  • 这适用于 Spring Web 以及在您的控制器中,您可以拥有 HttpServletRequest 类型的参数,它也适用于 MockHttpServletRequest 以及 Mock MVC 单元测试。
【解决方案9】:

在 Android 上,包 android.net 中有一个 Uri 类。请注意,Uriandroid.net 的一部分,而 URIjava.net 的一部分。

Uri 类有许多函数可以从查询中提取键值对。

以下函数以HashMap的形式返回键值对。

在 Java 中:

Map<String, String> getQueryKeyValueMap(Uri uri){
    HashMap<String, String> keyValueMap = new HashMap();
    String key;
    String value;

    Set<String> keyNamesList = uri.getQueryParameterNames();
    Iterator iterator = keyNamesList.iterator();

    while (iterator.hasNext()){
        key = (String) iterator.next();
        value = uri.getQueryParameter(key);
        keyValueMap.put(key, value);
    }
    return keyValueMap;
}

在 Kotlin 中:

fun getQueryKeyValueMap(uri: Uri): HashMap<String, String> {
        val keyValueMap = HashMap<String, String>()
        var key: String
        var value: String

        val keyNamesList = uri.queryParameterNames
        val iterator = keyNamesList.iterator()

        while (iterator.hasNext()) {
            key = iterator.next() as String
            value = uri.getQueryParameter(key) as String
            keyValueMap.put(key, value)
        }
        return keyValueMap
    }

【讨论】:

    【解决方案10】:

    如果您使用的是 Java 8,并且愿意编写一些可重用的方法,则可以一行完成。

    private Map<String, List<String>> parse(final String query) {
        return Arrays.asList(query.split("&")).stream().map(p -> p.split("=")).collect(Collectors.toMap(s -> decode(index(s, 0)), s -> Arrays.asList(decode(index(s, 1))), this::mergeLists));
    }
    
    private <T> List<T> mergeLists(final List<T> l1, final List<T> l2) {
        List<T> list = new ArrayList<>();
        list.addAll(l1);
        list.addAll(l2);
        return list;
    }
    
    private static <T> T index(final T[] array, final int index) {
        return index >= array.length ? null : array[index];
    }
    
    private static String decode(final String encoded) {
        try {
            return encoded == null ? null : URLDecoder.decode(encoded, "UTF-8");
        } catch(final UnsupportedEncodingException e) {
            throw new RuntimeException("Impossible: UTF-8 is a required encoding", e);
        }
    }
    

    但这是一条相当残酷的台词。

    【讨论】:

      【解决方案11】:

      Netty 还提供了一个很好的查询字符串解析器,称为QueryStringDecoder。 在一行代码中,它可以解析问题中的 URL。 我喜欢,因为它不需要捕捉或抛出java.net.MalformedURLException

      一行:

      Map<String, List<String>> parameters = new QueryStringDecoder(url).parameters();
      

      在此处查看 javadocs:https://netty.io/4.1/api/io/netty/handler/codec/http/QueryStringDecoder.html

      这是一个简短的、独立的、正确的例子:

      import io.netty.handler.codec.http.QueryStringDecoder;
      import org.apache.commons.lang3.StringUtils;
      
      import java.util.List;
      import java.util.Map;
      
      public class UrlParse {
      
        public static void main(String... args) {
          String url = "https://google.com.ua/oauth/authorize?client_id=SS&response_type=code&scope=N_FULL&access_type=offline&redirect_uri=http://localhost/Callback";
          QueryStringDecoder decoder = new QueryStringDecoder(url);
          Map<String, List<String>> parameters = decoder.parameters();
          print(parameters);
        }
      
        private static void print(final Map<String, List<String>> parameters) {
          System.out.println("NAME               VALUE");
          System.out.println("------------------------");
          parameters.forEach((key, values) ->
              values.forEach(val ->
                  System.out.println(StringUtils.rightPad(key, 19) + val)));
        }
      }
      

      生成

      NAME               VALUE
      ------------------------
      client_id          SS
      response_type      code
      scope              N_FULL
      access_type        offline
      redirect_uri       http://localhost/Callback
      

      【讨论】:

        【解决方案12】:

        使用上述 cmets 和解决方案,我使用 Map 存储所有查询参数,其中对象可以是字符串或 Set。下面给出解决方案。建议先使用某种 url 验证器来验证 url,然后调用 convertQueryStringToMap 方法。

        private static final String DEFAULT_ENCODING_SCHEME = "UTF-8";
        
        public static Map<String, Object> convertQueryStringToMap(String url) throws UnsupportedEncodingException, URISyntaxException {
            List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), DEFAULT_ENCODING_SCHEME);
            Map<String, Object> queryStringMap = new HashMap<>();
            for(NameValuePair param : params){
                queryStringMap.put(param.getName(), handleMultiValuedQueryParam(queryStringMap, param.getName(), param.getValue()));
            }
            return queryStringMap;
        }
        
        private static Object handleMultiValuedQueryParam(Map responseMap, String key, String value) {
            if (!responseMap.containsKey(key)) {
                return value.contains(",") ? new HashSet<String>(Arrays.asList(value.split(","))) : value;
            } else {
                Set<String> queryValueSet = responseMap.get(key) instanceof Set ? (Set<String>) responseMap.get(key) : new HashSet<String>();
                if (value.contains(",")) {
                    queryValueSet.addAll(Arrays.asList(value.split(",")));
                } else {
                    queryValueSet.add(value);
                }
                return queryValueSet;
            }
        }
        

        【讨论】:

        • 对于参数顺序很重要的用例,Set 是错误的数据类型。
        【解决方案13】:

        我试用了 Kotlin 版本,看看这是如何在 Google 中排名第一的。

        @Throws(UnsupportedEncodingException::class)
        fun splitQuery(url: URL): Map<String, List<String>> {
        
            val queryPairs = LinkedHashMap<String, ArrayList<String>>()
        
            url.query.split("&".toRegex())
                    .dropLastWhile { it.isEmpty() }
                    .map { it.split('=') }
                    .map { it.getOrEmpty(0).decodeToUTF8() to it.getOrEmpty(1).decodeToUTF8() }
                    .forEach { (key, value) ->
        
                        if (!queryPairs.containsKey(key)) {
                            queryPairs[key] = arrayListOf(value)
                        } else {
        
                            if(!queryPairs[key]!!.contains(value)) {
                                queryPairs[key]!!.add(value)
                            }
                        }
                    }
        
            return queryPairs
        }
        

        以及扩展方法

        fun List<String>.getOrEmpty(index: Int) : String {
            return getOrElse(index) {""}
        }
        
        fun String.decodeToUTF8(): String { 
            URLDecoder.decode(this, "UTF-8")
        }
        

        【讨论】:

        【解决方案14】:

        URI查询部分解码的即用型解决方案(包括解码和多参数值)

        评论

        我对@Pr0gr4mm3r 在https://stackoverflow.com/a/13592567/1211082 中提供的代码不满意。基于 Stream 的解决方案不做 URLDecoding,可变版本笨拙。

        因此我制定了一个解决方案

        • 可以将 URI 查询部分分解为 Map&lt;String, List&lt;Optional&lt;String&gt;&gt;&gt;
        • 可以处理多个值相同的参数名称
        • 可以正确表示没有值的参数Optional.empty() 而不是null
        • 通过URLdecode正确解码参数名称
        • 基于 Java 8 流
        • 可直接使用(请参阅下面包含导入的代码)
        • 允许正确的错误处理(这里通过将检查的异常UnsupportedEncodingException 转换为运行时异常RuntimeUnsupportedEncodingException,允许与流相互作用。(将常规函数包装到抛出检查异常的函数中是一种痛苦。而 Scala Try 是Java 语言默认不可用。)

        Java 代码

        import java.io.UnsupportedEncodingException;
        import java.net.URLDecoder;
        import java.util.*;
        import static java.util.stream.Collectors.*;
        
        public class URIParameterDecode {
            /**
             * Decode parameters in query part of a URI into a map from parameter name to its parameter values.
             * For parameters that occur multiple times each value is collected.
             * Proper decoding of the parameters is performed.
             * 
             * Example
             *   <pre>a=1&b=2&c=&a=4</pre>
             * is converted into
             *   <pre>{a=[Optional[1], Optional[4]], b=[Optional[2]], c=[Optional.empty]}</pre>
             * @param query the query part of an URI 
             * @return map of parameters names into a list of their values.
             *         
             */
            public static Map<String, List<Optional<String>>> splitQuery(String query) {
                if (query == null || query.isEmpty()) {
                    return Collections.emptyMap();
                }
        
                return Arrays.stream(query.split("&"))
                            .map(p -> splitQueryParameter(p))
                            .collect(groupingBy(e -> e.get0(), // group by parameter name
                                    mapping(e -> e.get1(), toList())));// keep parameter values and assemble into list
            }
        
            public static Pair<String, Optional<String>> splitQueryParameter(String parameter) {
                final String enc = "UTF-8";
                List<String> keyValue = Arrays.stream(parameter.split("="))
                        .map(e -> {
                            try {
                                return URLDecoder.decode(e, enc);
                            } catch (UnsupportedEncodingException ex) {
                                throw new RuntimeUnsupportedEncodingException(ex);
                            }
                        }).collect(toList());
        
                if (keyValue.size() == 2) {
                    return new Pair(keyValue.get(0), Optional.of(keyValue.get(1)));
                } else {
                    return new Pair(keyValue.get(0), Optional.empty());
                }
            }
        
            /** Runtime exception (instead of checked exception) to denote unsupported enconding */
            public static class RuntimeUnsupportedEncodingException extends RuntimeException {
                public RuntimeUnsupportedEncodingException(Throwable cause) {
                    super(cause);
                }
            }
        
            /**
             * A simple pair of two elements
             * @param <U> first element
             * @param <V> second element
             */
            public static class Pair<U, V> {
                U a;
                V b;
        
                public Pair(U u, V v) {
                    this.a = u;
                    this.b = v;
                }
        
                public U get0() {
                    return a;
                }
        
                public V get1() {
                    return b;
                }
            }
        }
        

        Scala 代码

        ...为了完整起见,我忍不住在 Scala 中提供以简洁和美观为主的解决方案

        import java.net.URLDecoder
        
        object Decode {
          def main(args: Array[String]): Unit = {
            val input = "a=1&b=2&c=&a=4";
            println(separate(input))
          }
        
          def separate(input: String) : Map[String, List[Option[String]]] = {
            case class Parameter(key: String, value: Option[String])
        
            def separateParameter(parameter: String) : Parameter =
              parameter.split("=")
                       .map(e => URLDecoder.decode(e, "UTF-8")) match {
              case Array(key, value) =>  Parameter(key, Some(value))
              case Array(key) => Parameter(key, None)
            }
        
            input.split("&").toList
              .map(p => separateParameter(p))
              .groupBy(p => p.key)
              .mapValues(vs => vs.map(p => p.value))
          }
        }
        

        【讨论】:

          【解决方案15】:

          Kotlin 的答案最初来自 https://stackoverflow.com/a/51024552/3286489,但通过整理代码并提供了 2 个版本的改进版本,并使用不可变的集合操作

          使用java.net.URI 提取查询。然后使用下面提供的扩展功能

          1. 假设您只想要查询的最后一个值,即page2&amp;page3 将得到{page=3},请使用以下扩展函数
              fun URI.getQueryMap(): Map<String, String> {
                  if (query == null) return emptyMap()
          
                  return query.split("&")
                          .mapNotNull { element -> element.split("=")
                                  .takeIf { it.size == 2 && it.none { it.isBlank() } } }
                          .associateBy({ it[0].decodeUTF8() }, { it[1].decodeUTF8() })
              }
          
              private fun String.decodeUTF8() = URLDecoder.decode(this, "UTF-8") // decode page=%22ABC%22 to page="ABC"
          
          1. 假设您想要查询的所有值的列表,即page2&amp;page3 将得到{page=[2, 3]}
              fun URI.getQueryMapList(): Map<String, List<String>> {
                  if (query == null) return emptyMap()
          
                  return query.split("&")
                          .distinct()
                          .mapNotNull { element -> element.split("=")
                                  .takeIf { it.size == 2 && it.none { it.isBlank() } } }
                          .groupBy({ it[0].decodeUTF8() }, { it[1].decodeUTF8() })
              }
          
              private fun String.decodeUTF8() = URLDecoder.decode(this, "UTF-8") // decode page=%22ABC%22 to page="ABC"
          

          使用方法如下

              val uri = URI("schema://host/path/?page=&page=2&page=2&page=3")
              println(uri.getQueryMapList()) // Result is {page=[2, 3]}
              println(uri.getQueryMap()) // Result is {page=3}
          

          【讨论】:

            【解决方案16】:

            正如您在查询具有单个参数定义时所指出的那样,有很多答案适用于您的查询。在某些应用程序中,处理一些额外的查询参数边缘情况可能很有用,例如:

            • param1&amp;param1=value&amp;param1=等参数值列表,意思是param1设置为List.of("", "value", "")
            • 无效排列,例如querypath?&amp;=&amp;&amp;=noparamname&amp;
            • 在映射a= 中使用不为空的空字符串表示“a”是List.of("") 以匹配Web servlet 处理

            这使用带有过滤器和 groupingBy 的 Stream 来收集到 Map&lt;String, List&lt;String&gt;&gt;

            public static Map<String, List<String>> getParameterValues(URL url) {
                return Arrays.stream(url.getQuery().split("&"))
                        .map(s -> s.split("="))
                        // filter out empty parameter names (as in Tomcat) "?&=&&=value&":
                        .filter(arr -> arr.length > 0 && arr[0].length() > 0)
                        .collect(Collectors.groupingBy(arr -> URLDecoder.decode(arr[0], StandardCharsets.UTF_8),
                                 // drop this line for not-name definition order Map:
                                 LinkedHashMap::new, 
                                 Collectors.mapping(arr -> arr.length < 2 ? "" : URLDecoder.decode(arr[1], StandardCharsets.UTF_8), Collectors.toList())));
            }
            

            【讨论】:

              【解决方案17】:

              如果您使用的是 Spring,请将 @RequestParam Map&lt;String,String&gt; 类型的参数添加到您的控制器方法中,Spring 将为您构建地图!

              【讨论】:

                【解决方案18】:

                只是对 Java 8 版本的更新

                public Map<String, List<String>> splitQuery(URL url) {
                    if (Strings.isNullOrEmpty(url.getQuery())) {
                        return Collections.emptyMap();
                    }
                    return Arrays.stream(url.getQuery().split("&"))
                            .map(this::splitQueryParameter)
                            .collect(Collectors.groupingBy(SimpleImmutableEntry::getKey, LinkedHashMap::new, **Collectors**.mapping(Map.Entry::getValue, **Collectors**.toList())));
                }
                

                mapping 和 toList() 方法必须与顶级答案中未提及的收集器一起使用。否则会在 IDE 中抛出编译错误

                【讨论】:

                • 您似乎还需要分享您的splitQueryParameters() 方法? **Collectors** 是怎么回事?
                【解决方案19】:

                在这里回答是因为这是一个热门话题。这是 Kotlin 中使用推荐的 UrlQuerySanitizer api 的干净解决方案。 See the official documentation。我添加了一个字符串生成器来连接和显示参数。

                    var myURL: String? = null
                
                    if (intent.hasExtra("my_value")) {
                        myURL = intent.extras.getString("my_value")
                    } else {
                        myURL = intent.dataString
                    }
                
                    val sanitizer = UrlQuerySanitizer(myURL)
                    // We don't want to manually define every expected query *key*, so we set this to true
                    sanitizer.allowUnregisteredParamaters = true
                    val parameterNamesToValues: List<UrlQuerySanitizer.ParameterValuePair> = sanitizer.parameterList
                    val parameterIterator: Iterator<UrlQuerySanitizer.ParameterValuePair> = parameterNamesToValues.iterator()
                
                    // Helper simply so we can display all values on screen
                    val stringBuilder = StringBuilder()
                
                    while (parameterIterator.hasNext()) {
                        val parameterValuePair: UrlQuerySanitizer.ParameterValuePair = parameterIterator.next()
                        val parameterName: String = parameterValuePair.mParameter
                        val parameterValue: String = parameterValuePair.mValue
                
                        // Append string to display all key value pairs
                        stringBuilder.append("Key: $parameterName\nValue: $parameterValue\n\n")
                    }
                
                    // Set a textView's text to display the string
                    val paramListString = stringBuilder.toString()
                    val textView: TextView = findViewById(R.id.activity_title) as TextView
                    textView.text = "Paramlist is \n\n$paramListString"
                
                    // to check if the url has specific keys
                    if (sanitizer.hasParameter("type")) {
                        val type = sanitizer.getValue("type")
                        println("sanitizer has type param $type")
                    }
                

                【讨论】:

                  【解决方案20】:

                  这是我使用 reduceOptional 的解决方案:

                  private Optional<SimpleImmutableEntry<String, String>> splitKeyValue(String text) {
                      String[] v = text.split("=");
                      if (v.length == 1 || v.length == 2) {
                          String key = URLDecoder.decode(v[0], StandardCharsets.UTF_8);
                          String value = v.length == 2 ? URLDecoder.decode(v[1], StandardCharsets.UTF_8) : null;
                          return Optional.of(new SimpleImmutableEntry<String, String>(key, value));
                      } else
                          return Optional.empty();
                  }
                  
                  private HashMap<String, String> parseQuery(URI uri) {
                      HashMap<String, String> params = Arrays.stream(uri.getQuery()
                              .split("&"))
                              .map(this::splitKeyValue)
                              .filter(Optional::isPresent)
                              .map(Optional::get)
                              .reduce(
                                  // initial value
                                  new HashMap<String, String>(), 
                                  // accumulator
                                  (map, kv) -> {
                                       map.put(kv.getKey(), kv.getValue()); 
                                       return map;
                                  }, 
                                  // combiner
                                  (a, b) -> {
                                       a.putAll(b); 
                                       return a;
                                  });
                      return params;
                  }
                  
                  • 我忽略了重复的参数(我取最后一个)。
                  • 我稍后使用Optional&lt;SimpleImmutableEntry&lt;String, String&gt;&gt; 忽略垃圾
                  • 减少从一个空映射开始,然后在每个 SimpleImmutableEntry 上填充它

                  如果你问,reduce 在最后一个参数中需要这个奇怪的组合器,它只用于并行流。它的目标是合并两个中间结果(这里是 HashMap)。

                  【讨论】:

                    【解决方案21】:

                    如果您碰巧在类路径中有 cxf-core 并且您知道您没有重复的查询参数,您可能需要使用 UrlUtils.parseQueryString

                    【讨论】:

                      【解决方案22】:

                      Eclipse Jersey REST framework 通过UriComponent 支持这一点。示例:

                      import org.glassfish.jersey.uri.UriComponent;
                      
                      String uri = "https://google.com.ua/oauth/authorize?client_id=SS&response_type=code&scope=N_FULL&access_type=offline&redirect_uri=http://localhost/Callback";
                      MultivaluedMap<String, String> params = UriComponent.decodeQuery(URI.create(uri), true);
                      for (String key : params.keySet()) {
                        System.out.println(key + ": " + params.getFirst(key));
                      }
                      

                      【讨论】:

                        【解决方案23】:

                        如果只想要字符串中的 URL 后面的参数。然后以下代码将起作用。我只是假设简单的 URL。我的意思是没有严格和快速的检查和解码。就像在我的一个测试用例中一样,我得到了 Url,我知道我只需要参数的值。网址很简单。无需编码解码。

                        String location = "https://google.com.ua/oauth/authorize?client_id=SS&response_type=code&scope=N_FULL&access_type=offline&redirect_uri=http://localhost/Callback";
                        String location1 = "https://stackoverflow.com?param1=value1&param2=value2&param3=value3";
                        String location2 = "https://stackoverflow.com?param1=value1&param2=&param3=value3&param3";
                            
                            Map<String, String> paramsMap = Stream.of(location)
                                .filter(l -> l.indexOf("?") != -1)
                                .map(l -> l.substring(l.indexOf("?") + 1, l.length()))
                                .flatMap(q -> Pattern.compile("&").splitAsStream(q))
                                .map(s -> s.split("="))
                                .filter(a -> a.length == 2)
                                .collect(Collectors.toMap(
                                    a -> a[0], 
                                    a -> a[1],
                                    (existing, replacement) -> existing + ", " + replacement,
                                    LinkedHashMap::new
                                ));
                            
                            System.out.println(paramsMap);
                        

                        谢谢

                        【讨论】:

                          猜你喜欢
                          • 2012-05-01
                          • 1970-01-01
                          • 1970-01-01
                          • 2021-10-28
                          • 1970-01-01
                          • 1970-01-01
                          • 2023-04-03
                          • 2021-03-15
                          相关资源
                          最近更新 更多