图片读取本地数据库路径转成二进制base64编码形式返回给前端

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
在这里插入图片描述

发布了32 篇原创文章 · 获赞 13 · 访问量 1526

猜你喜欢

转载自blog.csdn.net/weixin_38068605/article/details/103482642