base64url in java

前端 未结 10 601
攒了一身酷
攒了一身酷 2020-12-05 06:49

https://web.archive.org/web/20110422225659/https://en.wikipedia.org/wiki/Base64#URL_applications

talks about base64Url - Decode


a modified Base64 for U

10条回答
  •  盖世英雄少女心
    2020-12-05 07:26

    @ufk's answer works, but you don't actually need to set the urlSafe flag when you're just decoding.

    urlSafe is only applied to encode operations. Decoding seamlessly handles both modes.

    Also, there are some static helpers to make it shorter and more explicit:

    import org.apache.commons.codec.binary.Base64;
    import org.apache.commons.codec.binary.StringUtils;
    
    public static String base64UrlDecode(String input) {
      StringUtils.newStringUtf8(Base64.decodeBase64(input));
    }
    

    Docs

    • newStringUtf8()
    • decodeBase64()

提交回复
热议问题