deviceMapper.java
/*
*根据id读取图片
* */
public String ReadPicture(Map<String,Object> map);
deviceService.java
/**
*
* @param id(根据插入的id识别)
* @return
* @throws IOException
*/
public Object ReadPicture(String id) throws IOException {
byte[] temp = null;
Map<String,Object> map=new HashMap<String, Object>();
map.put("id",id);
String path=deviceMapper.ReadPicture(map);
try{
File file = new File(path);//注意此处读取的数据库路径
//应该是自己图片存储的路径C:\Users\Administrator\Desktop\test\001\2019.png
FileInputStream fis = new FileInputStream(file);
long size = file.length();
temp = new byte[(int) size];
/*
* b - 读取数据的缓冲区。
off - 目标数组 b的起始偏移量
len - 读取的最大字节数。
* */
fis.read(temp, 0, (int) size);
fis.close();
}catch (IOException e){
e.printStackTrace();
}
String baseStr = "data:image/jpeg;base64:" + Base64.getEncoder().encodeToString(temp);
return baseStr;
}
结果图是:
将编码复制出来
其中的base64