1、java
public static String ImageToBase64(String src){ if (src == "" || src == null) { return ""; } File file = new File(src); if (!file.exists()) { return ""; } byte [] data = null; try { FileInputStream fis = new FileInputStream(src); data = new byte[fis.available()]; fis.read(data); fis.close(); } catch (IOException e) { e.printStackTrace(); } BASE64Encoder base64Encoder = new BASE64Encoder(); return base64Encoder.encode(data); }
2、js
$("#showPic").append("<img class="layui-upload-img" id="showPic" src="data:image/png;base64,"+ src1 + "" height='40px' width='120px'>");
格式:
<img src="data:image/png;base64,从后台获取的base编码" />