springboot框架前端页面显示数据库中图片
时间: 2023-09-10 19:15:12 浏览: 198
在Spring Boot中,可以使用Thymeleaf模板引擎来渲染前端页面,并通过Controller层将数据库中的图片数据传递给前端页面进行展示。
以下是实现步骤:
1. 创建一个实体类,其中包含一个用于存储图片数据的byte[]类型的属性。
2. 在Controller层中定义一个方法,用于获取数据库中的图片数据并将其传递给前端页面。
```java
@GetMapping("/image")
public ResponseEntity<byte[]> getImage() {
byte[] imageData = imageService.getImageData(); //从数据库中获取图片数据
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG); //设置图片类型
return new ResponseEntity<>(imageData, headers, HttpStatus.OK);
}
```
3. 在前端页面中使用Thymeleaf模板引擎来渲染图片。
```html
<img th:src="@{/image}" alt="image">
```
该代码会向上述Controller层中的方法发送请求,获取图片数据并在页面中渲染出来。
需要注意的是,上述代码仅适用于JPEG格式的图片。如果需要支持其他格式的图片,需要相应地修改代码中的MediaType。