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 ...
随机推荐
- MOCK 基本使用例子
package com.icil.esolution.orders; import static org.springframework.test.web.servlet.request.MockMv ...
- Node + H5 + WebSocket + Koa2 实现简单的多人聊天
服务器代码 ( 依赖于 koa2, koa-websocket ) /* 实例化外部依赖 */ let Koa = require("koa2"); let WebSocket ...
- leetcode127
class Solution { public int ladderLength(String beginWord, String endWord, List<String> wordLi ...
- 怎样把代码复制到word中并保持颜色
- CentOS 7 JDK安装
官网: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 1.下载(下图有个错误 ...
- AS3 - 数组元素乱序方法以及效率比较
http://www.hangge.com/blog/cache/detail_453.html
- 9 并发编程-(线程)-守护线程&互斥锁
一 .守护线程 无论是进程还是线程,都遵循:守护xxx会等待主xxx运行完毕后被销毁 需要强调的是:运行完毕并非终止运行 1.对主进程来说,运行完毕指的是主进程代码运行完毕 2.对主线程来说,运行完毕 ...
- visibility和display
visibility: hidden----将元素隐藏,但是在网页中该占的位置还是占着.display: none----将元素的显示设为无,即在网页中不占任何的位置.
- SQL语句查询年龄分段分组查询
此情况用于数据库中没有“年龄”这个字段,只有“出生日期”这个字段.先计算出“年龄”,在分组查询. 1.SELECT *, ROUND(DATEDIFF(CURDATE(), popBirthday)/ ...
- Rquest对象代码练习
1.代码练习 <%@ page language="java" import="java.util.*" pageEncoding="utf-8 ...