类 Base64Utils


  • public abstract class Base64Utils
    extends Object
    A simple utility class for Base64 encoding and decoding.

    Adapts to either Java 8's Base64 class or Apache Commons Codec's Base64 class. With neither Java 8 nor Commons Codec present, encode(byte[])/decode(byte[]) calls will throw an IllegalStateException. However, as of Spring 4.2, encodeToString(byte[]) and decodeFromString(java.lang.String) will nevertheless work since they can delegate to the JAXB DatatypeConverter as a fallback. However, this does not apply when using the "UrlSafe" methods for RFC 4648 "URL and Filename Safe Alphabet"; a delegate is required.

    Note: Apache Commons Codec does not add padding (=) when encoding with the URL and Filename Safe Alphabet.

    从以下版本开始:
    4.1
    作者:
    Juergen Hoeller, Gary Russell
    另请参阅:
    Base64, Base64, javax.xml.bind.DatatypeConverter#printBase64Binary, javax.xml.bind.DatatypeConverter#parseBase64Binary
    • 方法详细资料

      • encode

        public static byte[] encode​(byte[] src)
        Base64-encode the given byte array.
        参数:
        src - the original byte array (may be null)
        返回:
        the encoded byte array (or null if the input was null)
        抛出:
        IllegalStateException - if Base64 encoding between byte arrays is not supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime
      • decode

        public static byte[] decode​(byte[] src)
        Base64-decode the given byte array.
        参数:
        src - the encoded byte array (may be null)
        返回:
        the original byte array (or null if the input was null)
        抛出:
        IllegalStateException - if Base64 encoding between byte arrays is not supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime
      • encodeUrlSafe

        public static byte[] encodeUrlSafe​(byte[] src)
        Base64-encode the given byte array using the RFC 4648 "URL and Filename Safe Alphabet".
        参数:
        src - the original byte array (may be null)
        返回:
        the encoded byte array (or null if the input was null)
        抛出:
        IllegalStateException - if Base64 encoding between byte arrays is not supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime
        从以下版本开始:
        4.2.4
      • decodeUrlSafe

        public static byte[] decodeUrlSafe​(byte[] src)
        Base64-decode the given byte array using the RFC 4648 "URL and Filename Safe Alphabet".
        参数:
        src - the encoded byte array (may be null)
        返回:
        the original byte array (or null if the input was null)
        抛出:
        IllegalStateException - if Base64 encoding between byte arrays is not supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime
        从以下版本开始:
        4.2.4
      • encodeToString

        public static String encodeToString​(byte[] src)
        Base64-encode the given byte array to a String.
        参数:
        src - the original byte array (may be null)
        返回:
        the encoded byte array as a UTF-8 String (or null if the input was null)
      • decodeFromString

        public static byte[] decodeFromString​(String src)
        Base64-decode the given byte array from an UTF-8 String.
        参数:
        src - the encoded UTF-8 String (may be null)
        返回:
        the original byte array (or null if the input was null)
      • encodeToUrlSafeString

        public static String encodeToUrlSafeString​(byte[] src)
        Base64-encode the given byte array to a String using the RFC 4648 "URL and Filename Safe Alphabet".
        参数:
        src - the original byte array (may be null)
        返回:
        the encoded byte array as a UTF-8 String (or null if the input was null)
        抛出:
        IllegalStateException - if Base64 encoding between byte arrays is not supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime
      • decodeFromUrlSafeString

        public static byte[] decodeFromUrlSafeString​(String src)
        Base64-decode the given byte array from an UTF-8 String using the RFC 4648 "URL and Filename Safe Alphabet".
        参数:
        src - the encoded UTF-8 String (may be null)
        返回:
        the original byte array (or null if the input was null)
        抛出:
        IllegalStateException - if Base64 encoding between byte arrays is not supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime