0

I have the need to encode a file as a data URI in Java, is it possible for me to retrieve this in the following format?

data:application/octet-stream;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA==

I've been looking for something like it, but had no luck so far.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
eBergamo
  • 101
  • 3
  • 6

1 Answers1

1

I don't think there is a utility function specifically for creating it, but it should be as simple as:

import org.apache.commons.codec.binary.Base64;

byte[] toEncode = { 0xaa, 0xbb, 0xcc };
Base64 base64NoLineWrap = new Base64(0);
String encodedData = base64NoLineWrap.encodeBase64String(toEncode);
String dataUri = "data:application/octet-stream;base64," + encoded;
John Watts
  • 8,717
  • 1
  • 31
  • 35