//这是个简单的小案例—用MySQL 数据库以二进制形式存储图片
我们数据库的类型应为:mediumblob 类型
原因:为了防止保存进去的图片超出存储的容量界线用mediumblob,它的最大存储量是16M;
MySQL中,blob是一个二进制大型对象,是一个可以存储大量数据的容器,它能容纳不同大小的数据。blob类型实际是个类型系列(tinyblob、blob、mediumblob、longblob),除了在存储的最大信息量上不同外,他们是等同的。
类型 | 大小(字节—最大) |
---|---|
tinyblob | 255 |
blob | 65K |
mediumblob | 16M |
longblob | 4G |
下面是案例的一个代码展示
public class TestSaveImager {
public static Connection con = null;
public static PreparedStatement ps = null;
public static ResultSet rs = null;
public static void testSave(){
try {
//这是一个自定义的数据
File file = new File("C:\\Users\\Administrator\\Downloads\\20190622063517.png");
FileInputStream inputStream = new FileInputStream(file);
con = DBUtil.getConnection();
ps = con.prepareStatement("insert into student(userid,name,sex,age,imager) values(?,?,?,?,?)")