What is the proper way to convert a byte [] to a Base64 string in Java? Better yet would be Grails / Groovy because it tells me that the encodeAsBase64()
function is deprecated. The sun.misc.BASE64Encoder
package isn't recommended for use and outputs a different size string on some Windows platforms.
相关问题
- Views base64 encoded blob in HTML with PHP
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
Implement your own method like this :)
}
(adding this to this thread in the hopes that somebody else will get a hit on this and doesn't have to waste his valuable time)
I got stymied today when I tried to add in my Grails 2.3.11/Groovy 2.1.9 application the output of
as a
data-
attribute to a DOM element. But theatob()
in the corresponding JavaScript, i.e. the code that decodes the Base64 string from the data attribute, kept complaining about illegal characters, while other decoders, e.g.base64 -d
accepted the same Base64 string without problems.The solution is to force the
render()
return value to a single string and then apply the Base64 encoding, i.e.or (if you consider
encodeAsBase64()
as deprecated):The preferred way to do this in groovy is:
You could use the open source Base64Coder library
Apache Commons has many utilities:
Binary Package: http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html
Download: http://commons.apache.org/codec/download_codec.cgi