controller层从数据库中读取图片
时间: 2024-04-29 11:24:14 浏览: 8
作为AI助手,我无法确定您想要读取哪种类型的图片。以下是一些常见的图片类型,以及读取图片的控制器层代码示例。
1. 读取PNG图片
```
@RequestMapping(value = "/image/{imageName}.png", produces = MediaType.IMAGE_PNG_VALUE, method = RequestMethod.GET)
@ResponseBody
public byte[] getImage(@PathVariable String imageName) throws IOException {
InputStream in = getClass().getResourceAsStream("/images/" + imageName + ".png");
return IOUtils.toByteArray(in);
}
```
2. 读取JPEG图片
```
@RequestMapping(value = "/image/{imageName}.jpeg", produces = MediaType.IMAGE_JPEG_VALUE, method = RequestMethod.GET)
@ResponseBody
public byte[] getImage(@PathVariable String imageName) throws IOException {
InputStream in = getClass().getResourceAsStream("/images/" + imageName + ".jpeg");
return IOUtils.toByteArray(in);
}
```
3. 读取GIF图片
```
@RequestMapping(value = "/image/{imageName}.gif", produces = MediaType.IMAGE_GIF_VALUE, method = RequestMethod.GET)
@ResponseBody
public byte[] getImage(@PathVariable String imageName) throws IOException {
InputStream in = getClass().getResourceAsStream("/images/" + imageName + ".gif");
return IOUtils.toByteArray(in);
}
```
请注意,以上示例假定您的图片位于`/images`文件夹中。您需要将其替换为实际路径。
另外,这些示例使用了Spring的`IOUtils`类,您需要将其导入到您的项目中。