Nore
Nore

Reputation: 111

Convert from Base 64 String in Java

Is there any way to convert a Base64 String like you can in .NET using Java? How can I achieve this?

Here is the .NET example :

Convert.FromBase64String

Upvotes: 2

Views: 8067

Answers (3)

Jere
Jere

Reputation: 601

I find it curious that every time someone asks about base 64 encoding/decoding in Java the only answers given are either use the Apache codecs library, use the old com.sun classes which shouldn't be used or assume it is in the application server such as the one provided by Tomcat.

The standard JDK has provided base 64 support since the introduction of Jaxb 1.0 in the javax.xml.bind.DatatypeConverter class.

So if you are using Java 6 or newer (when Jaxb1.0 was added to the JDK directly, was available as a ref prior to that) you don't need to use a 3rd party component for base 64 support, you can use what is built into the JDK.

Upvotes: 5

Siva Charan
Siva Charan

Reputation: 18064

  1. Download "commons-codec-1.3" from the Jakara web site: Apache Commons Codec
  2. Place jar file in lib folder
  3. Pass the base64Message to the function to get the decoded bytes

byte[] decodedBytes = Base64.decode(base64Message);
String decodedString = new String(decodedBytes);

Upvotes: 1

Shashank Kadne
Shashank Kadne

Reputation: 8101

I think you want to decode a base64encoded String...you can use this..

BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(yourString.getBytes());

Upvotes: 0

Related Questions