Menu

Convert an image to Base64

Java, Webcenter By Oct 16, 2014 No Comments
This post is to show how to generate base64 string to an image file.Below is the method which will return the base64 string to the given image file.
The input for this method is the full path of the image.

public String getBase64ForFile(String inFile) throws Exception {
String base64 = “”;
System.out.println(“inFile: ” + inFile);
URL url = new URL(inFile);
InputStream fin = url.openStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int next = 0;
while ((next = fin.read()) != -1) {
bos.write(next);
}
bos.flush();
base64 = “” + org.kobjects.base64.Base64.encode(bos.toByteArray());
System.out.println(“Base64: ” + base64);

bos.close();
fin.close();
return base64;
}

No Comments

Leave a comment

Hi, Welcome here.
JOIN OUR NEWSLETTER
And get notified everytime we publish a new blog post.