Difference between base and url base64 coding in Java 8

The Java 8 Base64 library has two options that you can use to build the URI: “Main” and “URL” and the file name is safe. ”The documentation points to RFC 4648 Table 2 as an explanation of the differences.

After reading the specification, it’s still not clear to me what the practical difference is between the two encodings: are the standards widely supported? What specifically about browsers? Is URL encoding and file name safe for encoding data URIs? Are there known support limitations?

+2
source share
2 answers

The easiest way is to provide an example (IMHO):

    Base64.Encoder enc = Base64.getEncoder();
    Base64.Encoder encURL = Base64.getUrlEncoder();

    byte[] bytes = enc.encode("subjects?_d".getBytes());
    byte[] bytesURL = encURL.encode("subjects?_d".getBytes());

    System.out.println(new String(bytes)); // c3ViamVjdHM/X2Q=      notice the "/"
    System.out.println(new String(bytesURL)); // c3ViamVjdHM_X2Q=   notice the "_"

    Base64.Decoder dec = Base64.getDecoder();
    Base64.Decoder decURL = Base64.getUrlDecoder();

    byte[] decodedURL = decURL.decode(bytesURL);
    byte[] decoded = dec.decode(bytes);

    System.out.println(new String(decodedURL));
    System.out.println(new String(decoded));

, URL safe, - .

, , : toBase64 toBase64URL. :

+ / toBase64 - _ toBase64URL.

, , URI ?; - .

+6

, URI base64 "URL " URI, Chrome.

: data:text/plain;base64,TG9yZW0/aXBzdW0= Lorem?ipsum, URL- data:text/plain;base64,TG9yZW0_aXBzdW0= (ERR_INVALID_URL).

0

Source: https://habr.com/ru/post/1688912/


All Articles