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 ...
随机推荐
- leetcode210
public class Solution { //test case [1,0] public int[] findOrder(int numCourses, int[][] prerequisit ...
- leetcode415
public class Solution { public string AddStrings(string num1, string num2) { //判断num1和num2的长度,进行对齐 i ...
- ajax的一些小知识
eval()可以把一个字符串转化为本地的js代码来执行 <script type="text/javascript"> var str = "alert('h ...
- <、>&、<& 重定向符
<.>&.<& 重定向符 这三个命令也是管道命令,但它们一般不常用,你只需要知道一下就ok了,当然如果想仔细研究的话,可以自己查一下资料.(本人已查过,网上也查不到相 ...
- z-index层级顺序 opacity透明度 display: none 模态框实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- nmap扫描工具
1.NMap工具 主要功能:探测主机是否在线.扫描主机开放端口和嗅探网络服务,用于网络探测和安全扫描. NMap支持很多扫描技术,例如:UDP.TCPconnect().TCPSYN(半开扫描).ft ...
- Get与Post提交方式的区别
用 curl 测试 post 请求: curl -d "agentCode=RB&startDate=2017-07-01&endDate=2017-09-01& ...
- 静态网页开发技术-HTML
今天我重新复习了一下静态网页开发技术,概括如下. 一 .HTML文档结构与基本语法 :放置了标签的文本文档,可供浏览器解释执行的网页文件 1.注释标记 2.标记 3.属性 二.基本标记与使用 1.网页 ...
- 吴裕雄 实战python编程(3)
import requests from bs4 import BeautifulSoup url = 'http://www.baidu.com'html = requests.get(url)sp ...
- git pull 免密
linux下 在~/下, touch创建文件 .git-credentials, 用vim编辑此文件,输入内容格式: touch .git-credentials vim .git-credentia ...