Parsing a URL in Java

前端 未结 4 1942
我寻月下人不归
我寻月下人不归 2021-01-20 16:32

I am looking for an equivalent to PHP\'s \"parse_url\" function in Java. I am not running in Tomcat. I have query strings saved in a database that I\'m trying to b

4条回答
  •  终归单人心
    2021-01-20 16:44

    Here's something quick and dirty (have not compiled it, but you should get the idea.

    URL url = new URL("http://...");
    String query = url.getQuery();
    String paramStrings[] = query.split("\\&");
    HashMultiMap params = HashMultiMap.create(); // <== google guava class
    for (int i=0;iparamStrings.length;i++) {
        String parts[] = params[i].split("=");
        params.put(URLDecoder.decode(parts[0], "UTF-8"), URLDecoder.decode(parts[1], "UTF-8"));
    }
    
    Set paramVals = params.get("paramName");
    

    If you don't want to use the guava class, you can accomplish the same thing with some additional code, and a HashMap>

提交回复
热议问题