Mybatis Blob和String互转,实现文件上传等。
这样的代码网上有很多,但是本人亲测有bug,
下面是我写的代码。望参考
@MappedJdbcTypes(JdbcType.BLOB)
public class BlobAndStringTypeHandler 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 {
ByteArrayInputStream bis = null;
bis = new ByteArrayInputStream(parameter.getBytes());
ps.setBinaryStream(i, bis, parameter.getBytes().length); //网上都是直接paramter.length() 这样是不对的。 } @Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
Blob blob = rs.getBlob(columnName);
byte[] returnValue = null;
String result = null;
if (null != blob) {
returnValue = blob.getBytes(1, (int) blob.length());
}
//将取出的流对象转为utf-8的字符串对象
if (null != returnValue) {
result = new String(returnValue);
}
return result;
} @Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Blob blob = rs.getBlob(columnIndex);
byte[] returnValue = null;
String result = null;
if (null != blob) {
returnValue = blob.getBytes(1, (int) blob.length());
}
//将取出的流对象转为utf-8的字符串对象
if (null != returnValue) {
result = new String(returnValue);
}
return result;
} @Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Blob blob = cs.getBlob(columnIndex);
byte[] returnValue = null;
String result = null;
if (null != blob) {
returnValue = blob.getBytes(1, (int) blob.length());
}
//将取出的流对象转为utf-8的字符串对象
if (null != returnValue) {
result = new String(returnValue);
}
return result;
}
}
下面简单介绍下 String.length()和String.getBytes().length()的区别:
String.length();字符串的长度,一个中文一个长度,就是一个字符
String.getBytes().length(): 字符串包含字节的长度,一个中文两个长度,就是两个字节
Mybatis Blob和String互转,实现文件上传等。的更多相关文章
- ASP.NET 基础多文件上传
////多图上传 [HttpPost] public string duo() { ///.net 多文件上传 获取文件的集合 HttpFileCollection files = HttpConte ...
- SSM文件上传
**自己对于SSM文件上传的一些心得** 刚开始的时候也是在网上寻找一些简单的案例,可能我的这篇文章不是最好的,但是这些都是我自己慢慢的摸索以及自己的尝试的一些心得,希望对各位有所帮助. 其实文件的上 ...
- springboot 文件上传和下载
文件的上传和下载 1.文件上传 html页面代码如下 <form method="post" action="/file/upload1" enctype ...
- jsp---》》》新闻发布系统的项目跟踪+++++++文件上传
先来一个分层架构图: WeebRoot目录下的页面: 现在,此项目以实现登录,注销,新闻列表,编辑主题>>>> 先来登录部分的关键代码 index.jsp中的代码 userIn ...
- JSP文件上传,好烦啊、、
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- (29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】
文件上传主要分以下几个步骤: (1)新建maven java project: (2)在pom.xml加入相应依赖: (3)新建一个表单页面(这里使用thymleaf); (4)编写controlle ...
- vue ----element-ui 文件上传upload 组件 实现 及其后台
1.前台 action 不用改 :https://jsonplaceholder.typicode.com/posts/ getFile: 获取文件 data(){ return { file: {} ...
- SpringBoot基础实战系列(三)springboot单文件与多文件上传
springboot单文件上传 对于springboot文件上传需要了解一个类MultipartFile ,该类用于文件上传.我此次使用thymeleaf模板引擎,该模板引擎文件后缀 .html. 1 ...
- springMVC+spring+mybatis整合(包括文件上传和下载)
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncod ...
随机推荐
- Sum All Numbers in a Range(两数之间数字总和)
题目:我们会传递给你一个包含两个数字的数组.返回这两个数字和它们之间所有数字的和. 最小的数字并非总在最前面. /*方法一: 公式法 (首+末)*项数/2 */ /*两个数比较大小的函数*/ func ...
- (13/24) css进阶:自动处理css3属性前缀
什么是属性前缀 为了浏览器的兼容性,有时候我们必须加入-webkit,-ms,-o,-moz这些前缀.目的就是让我们写的页面在每个浏览器中都可以顺利运行. 例如: transition: width ...
- zabbix3.4.7 饼图显示问题
最近安装了zabbix3.4.7,发现系统自带Template OS Linux模版饼图(Pie)有两个问题: Total disk space on / 显示为 no data,也就是没有数据: 把 ...
- linux 使用随笔
目录:1,ab命令 一,ab命令 ab网站压力测试命令的参数.输出结果的中文注解 ab命令是Apache的Web服务器的性能测试工具,它可以测试安装Web服务器每秒种处理的HTTP请求. 来自: ht ...
- linux添加计划任务
crond 是linux用来定期执行程序的命令.当安装完成操作系统之后,默认便会启动此任务调度命令.crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作.可以用以下的 ...
- web.xml 组件加载顺序
<web-app> <display-name></display-name> WEB应用的名字 <description></descr ...
- style css
Title 语文 用户名 用户名 数学 英语 <!DOCTYPE html><html lang="en"><head> <meta ch ...
- 前端-javascript-BOM-浏览器对象模型
BOM的介绍---浏览器对象模型. 操作浏览器部分功能的API.比如让浏览器自动滚动. -------------------------------------------------------- ...
- Datatable数据分组
datatable里面的数据是按照这个顺序排列的 姓名 性别 年龄 a1 男 12 a1 女 11 a ...
- HDFS 好的文章链接
http://www.cnblogs.com/linuxprobe/p/5594431.html http://www.daniubiji.cn/archives/596 http://blog.cs ...