Java 将图片转换成base64编码,并展示在前台
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'>");