String urlEncoded = Base64.getUrlEncoder().encodeToString( url.getBytes( "utf-8" ) ) //output would be: c3ViamVjdHM_bWF0aGVtYXRpY3M= This alphabet does not use special characters used in URLs like ‘/’. It uses the URL and Filename safe base64 alphabet and does not add any line separation. This one is very similar to the basic encoder. String str = "España va muy bien and German uses ö, ä and ü" īyte decodedStr = Base64.getDecoder().decode( str ) Įxception in thread "main" : Illegal base64 character -fĪt 64$code0(Unknown Source)Īt 64$code(Unknown Source)Īt 64.Base64Examples.basicRejects(Base64Examples.java:97)Īt 64.Base64Examples.main(Base64Examples.java:20) We can see an example (again, is just an example, no practical use): The basic decoder rejects (and throws and IllegalArgumentException) data that contains characters that are not contained in the base64 alphabet like ö, ä or ñ. ( "Decoded using basic decoding " + new String( decodedStr, "utf-8" ) ) ĭecoded using basic decoding this is an string In order to decode this one, we can do something like:īyte decodedStr = Base64.getDecoder().decode( strEncoded ) If we print the string strEncoded out we would get: String strEncoded = Base64.getEncoder().encodeToString( str.getBytes( "utf-8" ) ) In order to encode a given String we can write: The basic encoder does not add any line separation and encodes using the Base64 alphabet as explained before. In the next chapters we are going to explain how to use the different kinds of encoders and decoders: Basic All code has been implemented using Java 8 update 11 and Eclipse Luna version 4.4. MIME type encoding: Uses the alphabet for encoding and decoding specified in the RFC 2045 and adds line separations.Īt the end of this article you can download all the examples and snippets (and more).URL based encoding: Uses the alphabet for URL and Filenames specified in the RFC 4648.Basic encoding: Uses for encoding/decoding the Base64 Alphabet as listed in the RFC 46.Now, the package java.util already contains a couple of classes that support this:īasically, these util classes offer a set of static methods with encoding and decoding support in the following modes: In old Java versions, it was needed to use external libraries like commons-codec or 64Decoder for encoding and decoding Strings. Until now, this was only possible by using external libraries, since Java update 8, encoding and decoding is part of the standard JRE. This article is about encoding Strings using Java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |