由于业务需要,需要从数据库中存放签名图片,采用blob格式存储,接口获取请求后,需从数据库中读取返回这个字段里的内容
采用byte[]的方式接受blob里的内容,图片需要转为base64,其他存放文本的blob需要转回正常的格式 工具类方法
java /**
     *
     * @Description:  二进制流转Base64字符串
     * @Param: byteArr
     * @author lihaowei
     * @since: 2023/10/26 11:32
     * @return String
     */
    public static String byteArr2String(byte[] byteArr) throws Exception {
        String stringBase64 = null;
        try {
            Base64 encoder = new Base64();
            stringBase64 =(byteArr != null ? encoder.encodeToString(byteArr) : "");
        } catch (Exception e) {
            throw new Exception("byteArr2String转换异常:"+e);
        }
        return stringBase64;
    }
具体功能
java
 List<RecMrDetailSignsResponse>  responses=new ArrayList<>();
        for (RecMrDetailSignsEntity e:list) {
            RecMrDetailSignsResponse r=BeanUtil.toBean(e,RecMrDetailSignsResponse.class);
            try {
                r.setPlainData(Base64Util.byteArr2String(e.getPlainData()));
                r.setSignData(new String(e.getSignData(),"GBK"));
                r.setSignTimeStamp(new String(e.getSignTimeStamp(),"GBK"));
            } catch (Exception ex) {
                ex.printStackTrace();
                throw new BusinessException(ResultCodeEnum.ERR_0x1000.getCode(),"签名信息转换出错");
            }
            responses.add(r);
        }
        return responses;
java    public String getCaSign(UserRequest request) {
        Object caObject=baseMapper.getCaSign(request);
        if (caObject==null){
            return null;
        }
        byte[] caBytes= new byte[0];
        try {
            caBytes = ((Blob) caObject).getBytes(1, (int) ((Blob) caObject).length());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String caStr = Base64.getEncoder().encodeToString(caBytes);
        return caStr;
    }
mapper接口
java
@Select("select t.SIGNATURE from ca t where t.user = #{request.userId}")
    Object getCaSign(@Param("request")UserRequest request);
javaimport java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
 
/**
 * Blob转String类型转换器
 */
public class Blob2StringTypeHandler extends BaseTypeHandler<String> {
 
    private static final String DEFAULT_CHARSET = "UTF-8";
    
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i,
            String parameter, JdbcType jdbcType) throws SQLException {
        if(jdbcType == JdbcType.BLOB){
            ByteArrayInputStream bis;
            try {
                byte[] b = parameter.getBytes(DEFAULT_CHARSET);
                bis = new ByteArrayInputStream(b);
                ps.setBinaryStream(i, bis, b.length);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("Blob Encoding Error!", e);
            }
        }else{
            ps.setString(i, parameter);
        }
    }
 
    @Override
    public String getNullableResult(ResultSet rs, String columnName)
            throws SQLException {
        try {
            Blob blob = rs.getBlob(columnName);
            byte[] returnValue = new byte[0];
            if (null != blob) {
                returnValue = blob.getBytes(1, (int) blob.length());
            }
            return new String(returnValue, DEFAULT_CHARSET);
        } catch (Exception e) {
            return rs.getString(columnName);
        }
    }
 
    @Override
    public String getNullableResult(ResultSet rs, int columnIndex)
            throws SQLException {
        try {
            Blob blob = rs.getBlob(columnIndex);
            byte[] returnValue = new byte[0];
            if (null != blob) {
                returnValue = blob.getBytes(1, (int) blob.length());
            }
            return new String(returnValue, DEFAULT_CHARSET);
        } catch (Exception e) {
            return rs.getString(columnIndex);
        }
    }
 
    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        Blob blob = cs.getBlob(columnIndex);
        byte[] returnValue = new byte[0];
        if (null != blob) {
            returnValue = blob.getBytes(1, (int) blob.length());
        }
        try {
            return new String(returnValue, DEFAULT_CHARSET);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Blob Encoding Error!", e);
        }
    }
 
}
yml配置
ymlmybatis:
  #  该配置项配置了MyBatis配置文件保存路径
  mapper-locations: classpath*:lhw/product/mapper/*.xml
  type-handlers-package: lhw.product.handler
mapper.xml中配置
xml <result typeHandler="com.lhw.product.handler.Blob2StringTypeHandler"  column="SIGN_TIME_STAMP" property="signTimeStamp" />
本文作者:Weee
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!