postgre存储bytea类型(blob)

简介: postgre存储bytea类型(blob)


image.png

1.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;
    }

2.entity

private Object content; //二进制流

3.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>

4.service

service不多说,对象传递即可。


相关文章
|
存储 编解码 监控
VOS3000作用与功能
VOS3000是一套VoIP 运营支撑系统,系统拥有强大的功能集合及可靠的安全保障,可支撑 5000 路并发,适用于话务转发,企业终端,APP 业务,大型呼叫中心调度等多种业务形态,满足了大部分高并发大容量 VoIP 业务的需求。
4298 0
|
关系型数据库 数据库 数据安全/隐私保护
Docker-10:Docker安装PostgreSQL
通过容器化Docker 安装 postgrel
7631 0
Docker-10:Docker安装PostgreSQL
|
关系型数据库 数据库 PostgreSQL
postgresql :permission denied to create database
postgresql :permission denied to create database
1706 0
|
存储 SQL 数据库
达梦(DM)数据库管理表
达梦(DM)数据库表管理
达梦(DM)数据库管理表
|
人工智能 安全 API
2026 WhatsApp Business API 落地攻略:功能、费用、申请流程、防封号指南
2026年,WhatsApp Business API 势必会成为跨境出海与全球化运营的标配。本文旨在为企业运营和营销人员提供一站式落地指南,深度解析 WhatsApp 三大版本的本质区别,并针对申请流程、计费逻辑、账号防封及常见故障提供实操建议。通过 Meta 官方合作伙伴阿里云,企业可实现 AI 自动化集成与合规的大规模客户触达。
7359 0
|
安全 JavaScript 前端开发
安全漏洞检测集成及实践:SAST/DAST工具集成指南
通过合理集成和配置SAST/DAST工具,可以显著提升应用程序的安全性,并在开发早期发现和修复漏洞,降低安全风险和维护成本
1438 4
|
JSON 算法 安全
harmony-utils之RSA,RSA加解密
harmony-utils是一款专为HarmonyOS打造的高效工具库,提供丰富的实用功能,助力开发者快速构建鸿蒙应用。其RSA模块实现非对称加解密、签名验签等功能,支持密钥生成、数据加密解密及分段处理,适用于安全通信与身份验证等场景。
576 0
|
开发工具 iOS开发 开发者
「Mac畅玩鸿蒙与硬件2」鸿蒙开发环境配置篇2 - 在Mac上安装DevEco Studio
本篇将专注于如何在 Mac 上安装鸿蒙开发工具 DevEco Studio,确保开发环境能够顺利搭建。完成安装后,可以正式开始鸿蒙应用的开发工作。
2018 1
「Mac畅玩鸿蒙与硬件2」鸿蒙开发环境配置篇2 - 在Mac上安装DevEco Studio
|
Shell 开发工具 git
git拉去代码报错"Failed to connect to 127.0.0.1 port 31181: Connection refused"
在克隆`https://example.git/`时遇到错误:&quot;fatal: unable to access &#39;https://example.git/&#39;: Failed to connect to 127.0.0.1 port 31181: Connection refused&quot;。原因是Windows上代理设置未正确关闭,影响了Git配置。解决方法是通过git bash运行命令检查并取消代理:`git config --global http.proxy`和`git config --global --unset http.proxy`。
1105 1
 git拉去代码报错"Failed to connect to 127.0.0.1 port 31181: Connection refused"