0

I get a PDF image as Base64Encoded bytes in an XML file, something like this(much longer, stripped):

JVBERi0xLjMKMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMyAwIFIKL091dGxpbmVzIDIgMCBSCi9EZXN0cyA1IDAgUgovUGFnZU1vZGUgL1VzZU5vbm

Want to write java code to convert it into image file . I tried using javax.xml.bind.DatatypeConverter.printBase64Binary(byte[]) but not sure how to create byte[] of above bytes.

gideon
  • 19,329
  • 11
  • 72
  • 113
Charu Khurana
  • 4,511
  • 8
  • 47
  • 81

1 Answers1

-1

You can use the sun.misc.BASE64Decoder

public byte[] fromBase64(String b64) {
  BASE64Decoder decoder = new BASE64Decoder();
  return decoder.decodeBuffer(b64)
}
Paul Rubel
  • 26,632
  • 7
  • 60
  • 80
  • my question is how do I pass bytes to methods. In this example, string is required to pass. In my original question, I used a method which takes byte[] but I don't know how to create byte[] out of my bytes – Charu Khurana Jun 10 '13 at 20:05
  • 5
    Do not use the package `sun.misc`. It's not part of Java SE and may change in future versions of Java. – Marlon Bernardes Jun 10 '13 at 20:07
  • @Learner. The above method takes in your string and creates a byte array, byte[]. That's how you get the bytes represented by your base-64-encoded string. – Paul Rubel Jun 10 '13 at 20:15
  • @MarlonBernandes Agree. Actually, JDK 8 finally brings Base64 encoder/decoder as a part of standard "tools". – Miljen Mikic Jun 10 '13 at 20:16