Wednesday, 15 June 2011

url - How to parse and decode URI in Java to URI components? -


I am trying to find a method that will parse a URL, decode it and decode it in a clear way Returns the components made / P>

is not the right fit, because it can return the obscure string, e.g.

  URLDecoder.decode ("http://www.google.com?q=abc% 26def", "UTF-8")  

Returns :

  http://www.google. Com? Q = abc & amp; Def  

then the information about avoiding & amp; Lost it.

I have to do something like this:

  decoded URL Components cmp = GreatURLDecoder.decode (url); Maps & lt; String, list & lt; String & gt; & Gt; DecodedQuery = cmp.getQuery (); . Find decodedQuery.get ("q") (0); // Returns "ABC and DF"  

How can I complete this?

EDIT: Thanks for the responses, but my question was slightly different: I would like to be able to get the decoded components in a clear way, then any of the following What do I need:

  • New URI ("http://www.google ABC% 26def
  • New URI ("Http: //www.google.com?q=abc%26def"). GetQuery () gives ambiguous values: q = abc and def
  • URLDecoder .decode ("http://www.google.com?q=abc%26def", "UTF-8") gives ambiguous values: http: // www. Google.com?q= abies & Amp; DEF
  • org.springframework.web.util.UriComponentsBuilder.fromUriString ("http://www.google.com?q=abc%26def") .build (True) .getQueryParams () - Closed, but still not what I want, because it returns the map of encoded parameters: {q = [abc% 26def]}

For example you can use the implementation. An example would be org.jboss.resteasy.spi.ResteasyUriInfo . If you are using Maven, you only need to add your pom.xml to the following:

  & lt; Dependency & gt; & Lt; Group & gt; Org.jboss.resteasy & lt; / Group & gt; & Lt; ArtifactId & gt; Resteasy-jaxrs & lt; / ArtifactId> & Lt; Version & gt; 3.0.6.Final & lt; / Edition & gt; & Lt; / Dependencies & gt;  

Then you should do the following code:

  UriInfo ui = New ResteasyUriInfo (new URI ("http://www.google.com? Q = ABC% 26def ")); & Lt; String & gt; QValues ​​= ui.getQueryParameters (). Get ("q"); For (string q: qValues) {System.out.println (q); }  

No comments:

Post a Comment