Here you can find the source of encodeToString(BufferedImage image, String type)
public static String encodeToString(BufferedImage image, String type)
//package com.java2s; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import sun.misc.BASE64Encoder; public class Main { public static String encodeToString(File image, String type) { BufferedImage originalImage = null; try {//www . j a v a 2 s.c o m originalImage = ImageIO.read(image); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return encodeToString(originalImage, type); } public static String encodeToString(BufferedImage image, String type) { String imageString = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ImageIO.write(image, type, bos); byte[] imageBytes = bos.toByteArray(); BASE64Encoder encoder = new BASE64Encoder(); imageString = encoder.encode(imageBytes); bos.close(); } catch (IOException e) { e.printStackTrace(); } return imageString; } }