1.工具类
package com.atguigu.yygh.common.util; import org.apache.commons.codec.binary.Base64; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; public class ImageBase64Util { public static void main(String[] args) { String imageFile= "G:\\0722test\\111.png";// 待处理的图片 System.out.println(getImageString(imageFile)); } public static String getImageString(String imageFile){ InputStream is = null; try { byte[] data = null; is = new FileInputStream(new File(imageFile)); data = new byte[is.available()]; is.read(data); return new String(Base64.encodeBase64(data)); } catch (Exception e) { e.printStackTrace(); } finally { if (null != is) { try { is.close(); is = null; } catch (Exception e) { e.printStackTrace(); } } } return ""; } }
测试
<img src="data:image/png;base64,iVBORw0..>,改文件名后缀