Java操作mysql存储图片

来源:互联网 发布:pl sql developer教程 编辑:程序博客网 时间:2024/05/18 13:07

http://bbs.chinaunix.net/archiver/tid-2289421.html

 

1 把图片当成一个二进制流就可以了。mysql中有可以存储很大的2进制流文件。用的类型是:

 

我们要做的是将一张图片存入Mysql中,在Mysql中用Blob 来存储图片和音频等大的数据项.Blob 按其容量可分为四种,分别为:tinyblob,blob ,mediumblob, longblob.他们的大小分别为:256B,64KB,16MB,4GB.除了容量不同外,这四种的用法一个样


2 将一个图片文件输入到mysql

FileInputStream fin = new FileInputStream(image);//生成的流


PreparedStatement ps = con.prepareStatement("insert into id_image values(?, ?)");//
ps.setInt(1, i);
ps.setBinaryStream(2, fin, len);

ps.executeUpdate();


插入就完成了。。

 

3 将一个图片读出来,以流的形式存放。

 

Statement stm = conn.createStatement();
String sql = "select * from id_image where id = "
                         + new Integer(id).toString();
ResultSet rs = stm.executeQuery(sql);

 

Blob blob = (Blob)rs.getBlob("image");

InputStream is = blob.getBinaryStream();

 

然后就可以利用is进行想要的操作了。

 

 

先用Blob格式存储,然后变为

 


原创粉丝点击