I have an image that is Base64 encoded. What is the best way to decode that in Java? Hopefully using only the libraries included with Sun Java 6.
相关问题
- 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
Given a test encode/decode example of javax.xml.bind.DatatypeConverter using methods parseBase64Binary() and printBase64Binary() referring to @jeremy-ross and @nightfirecat answer.
Result:
This is a late answer, but Joshua Bloch committed his
Base64
class (when he was working for Sun, ahem, Oracle) under thejava.util.prefs
package. This class existed since JDK 1.4.E.g.
In a code compiled with Java 7 but potentially running in a higher java version, it seems useful to detect presence of
java.util.Base64
class and use the approach best for given JVM mentioned in other questions here.I used this code:
No need to use commons--Sun ships a base64 encoder with Java. You can import it as such:
And then use it like this:
Where
encodedBytes
is either ajava.lang.String
or ajava.io.InputStream
. Just beware that thesun.*
classes are not "officially supported" by Sun.EDIT: Who knew this would be the most controversial answer I'd ever post? I do know that sun.* packages are not supported or guaranteed to continue existing, and I do know about Commons and use it all the time. However, the poster asked for a class that that was "included with Sun Java 6," and that's what I was trying to answer. I agree that Commons is the best way to go in general.
EDIT 2: As amir75 points out below, Java 6+ ships with JAXB, which contains supported code to encode/decode Base64. Please see Jeremy Ross' answer below.
If you are already adding AWS SDK for Java, you can use com.amazonaws.util.Base64.
I used
android.util.base64
that works pretty good without any dependances:Usage:
package com.test;