Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

SpringBoot将图片存储到数据库

存储方式

将图片转化为blob的二进制格式,然后存储到数据库即可,从数据中取出时,将blob格式的二进制文字从数据库中取出,用Object的方式来获取,然后转化为图片的格式,即可在屏幕上显示

上传图片

Controller层

1
2
3
4
5
6
7
8
9
10
11
12
13
@PostMapping("/add/image")
@ResponseBody
public String addImage(@RequestParam("file") MultipartFile file,@RequestParam("id") Integer id) throws Exception{
if(!file.isEmpty()){
BASE64Encoder encoder = new BASE64Encoder();
String image = encoder.encode(file.getBytes());
TestPo testPo = new TestPo();
testPo.setId(id);
testPo.setImage(image);
testService.addImage(testPo);
}
return "ok";
}

Controoler层主要对上传的图片文件进行处理,当上传的图片不为空的时候,定义一个字符串加密算法,然后将文件的字节编码进行加密,再定义一个实体类对象,将id以及加密之后的文件发送给业务层。

Service层

1
2
3
4
public String addImage(TestPo testPo){
testMapper.insertImage(testPo);
return "ok";
}

接受Controller的请求并返回对应的类型

Mapper接口

1
2
3
4
@Mapper
public interface TestMapper {
void insertImage(TestPo testPo);
}

Mapper.xml

1
2
3
<insert id = "insertImage" parameterType="cn.edu.guet.bean.TestPo">
insert into image(id,image) values(#{id},#{image})
</insert>

执行sql语句,进行文件的存储,存储为BLOB格式。

html页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8081/add/image" method="post" enctype="multipart/form-data">
<input type="text" name="id" /><br/>
<input type="file" name="file" /><br/>
<input type="submit" name="" id="" value="提交" />
</form>
</body>
</html>

image

读取图片

Controller层

1
2
3
4
5
@GetMapping("/get/image")
@ResponseBody
public void getImage(@RequestParam("id") Integer id, HttpServletResponse response) throws Exception{
testService.getImage(id,response);
}

主要是接收url请求,根据id给servce层发送请求

Service层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public String getImage(Integer id, HttpServletResponse response){
try {
TestPo testPo = testMapper.selectImageById(id);
byte[] image = (byte[])testPo.getImage();
String value = new String(image,"UTF-8");
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = decoder.decodeBuffer(value);
for(int i=0;i<bytes.length;i++){
if(bytes[i]<0){
bytes[i]+=256;
}
}
response.setContentType("image/jpeg");
ServletOutputStream out = response.getOutputStream();
out.write(bytes);
out.flush();
out.close();
}catch (Exception e){
e.printStackTrace();
}

return "ok";
}

service层对读取出来的二进制进行处理,用byte[]数据类型存储,然后将其转码成UTF-8的格式,此时我们可以得到想要的BASe64字节码,接着将其解码成为byte[]格式,只有这样才能生成图片,setContentType用于设置生成的图片的格式,然后通过流操作,将图片发送到Controller层,最后在前端显示出来

Maoopper接口

1
2
3
4
@Mapper
public interface TestMapper {
TestPo selectImageById(Integer id);
}

Mapper.xml

1
2
3
<select id="selectImageById" parameterType="Integer" resultType="cn.edu.guet.bean.TestPo">
select * from image where id = #{id}
</select>

html界面

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<img src="http://localhost:8081/get/image?id=2" alt="Responsive image">
</body>
</html>

image