public class ImageToBase64Demo {
public static void main(String[] args) throws IOException {
String imgPath = "1.jpg";
String imageToBase64 = ImageToBase64(imgPath);
}
private static String ImageToBase64(String imgPath) {
byte[] data = null;
//读取图片字节数组
try {
InputStream in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
//返回Base64编码过的字节数组字符串
return encoder.encode(Objects.requireNonNull(data));
}
}