postgresql实战技巧(二)postgre存储bytea类型(blob)
【摘要】
conrtoller
public Result<SysImageVO> uploadImg(SysImageVO sysImageVO, MultipartFile imgFile) throws IOException { Result<SysImageVO> result = new Res...
conrtoller
-
public Result<SysImageVO> uploadImg(SysImageVO sysImageVO, MultipartFile imgFile) throws IOException {
-
Result<SysImageVO> result = new Result<>();
-
sysImageVO.setContent(imgFile.getBytes());
-
sysImageService.uploadImg(sysImageVO);
-
return result;
-
}
-
-
-
public Result downloadImg(SysImageVO sysImageVO, HttpServletResponse response) throws IOException {
-
Result result = new Result();
-
// 业务处理
-
SysImageVO image = sysImageService.downloadImg(sysImageVO);
-
// 将content转化
-
byte[] content = (byte[]) image.getContent();
-
// 设置文件名
-
response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(image.getName(), "UTF-8") + ";");
-
// 设置文件大小
-
response.setContentLength(Integer.parseInt(image.getSize()));
-
// 将图片流通过response对象返回
-
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
-
bos.write(content);
-
bos.close();
-
return result;
-
}
entity
private Object content; //二进制流
mapper
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
<mapper namespace="com.vanpeng.resource.business.modular.image.dao.SysImageDao">
-
<resultMap type="xx.entity.ImageVO" id="ImageMap">
-
<result property="id" column="id" jdbcType="INTEGER"/>
-
<result property="name" column="name" jdbcType="VARCHAR"/>
-
<result property="content" column="content" jdbcType="BLOB"/> //bytea
-
</resultMap>
-
-
<!--新增图片-->
-
<insert id="uploadImg">
-
insert into sys_image(id, name, content)
-
values (#{id, jdbcType=INTEGER},#{name, jdbcType=VARCHAR}, #{content})
-
</insert>
-
-
<!--下载图片-->
-
<select id="getImgById" resultMap="ImageMap">
-
select
-
id, name, content
-
from sys_image
-
where id = #{id}
-
</select>
-
</mapper>
seriver不多说 对象传递即可
文章来源: baocl.blog.csdn.net,作者:小黄鸡1992,版权归原作者所有,如需转载,请联系作者。
原文链接:baocl.blog.csdn.net/article/details/115542019
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)