Search code examples
javautf-8decodeurldecode

UrlDecoder decode several times


Is there any method that fully decodes a String ? For example I have

monta%25C3%25B1a , if I use UrlDecoder.decode method ONCE : it returns monta%C3%B1a and if I decodify AGAIN , it finally returns montaña (that is the fully decodified string).Is there any method or library in Java that achieves this result?


Solution

  • monta[%25]C3[%25]B1a
    monta  %  C3  %  B1a    which has a UTF-8 multi-byte sequence
    monta       ñ      a
    

    It is important to decode with the same Charset as it was encoded. Evidently it was URL encoded twice, first into UTF-8, and then % was still encoded once.

    Twice doing the encoding should be repaired, as otherwise an incomprehensible patch remains:

    s = URLDecoder.decode(s, StandardCharsets.UTF_8);
    s = URLDecoder.decode(s, StandardCharsets.UTF_8);