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;
}
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:
Post a Comment