正常的图片储存要么放进本地磁盘,要么就存进数据库。存入本地很简单,现在我在这里记下如何将图片存进mysql数据库
如果要图片存进数据库 要将图片转化成二进制。
1.数据库存储图片的字段类型要为blob二进制大对象类型
2.将图片流转化为二进制
下面放上代码实例
一、数据库
create table `photo` ( `id` int(11) not null, `name` varchar(255) default null, `photo` blob, primary key (`id`) ) engine=innodb default charset=utf8;
二、数据库链接
/** * */ package jdbcimgtest; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; /** * @author administrator * */ public class dbutil { // 定义数据库连接参数 public static final string driver_class_name = "com.mysql.jdbc.driver"; public static final string url = "jdbc:mysql://localhost:3306/test"; public static final string username = "root"; public static final string password = "root"; // 注册数据库驱动 static { try { class.forname(driver_class_name); } catch (classnotfoundexception e) { system.out.println("注册失败!"); e.printstacktrace(); } } // 获取连接 public static connection getconn() throws sqlexception { return drivermanager.getconnection(url, username, password); } // 关闭连接 public static void closeconn(connection conn) { if (null != conn) { try { conn.close(); } catch (sqlexception e) { system.out.println("关闭连接失败!"); e.printstacktrace(); } } } //测试 /* public static void main(string[] args) throws sqlexception { system.out.println(dbutil.getconn()); } */ }
三、图片流
package jdbcimgtest; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; /** * @author administrator * */ public class imageutil { // 读取本地图片获取输入流 public static fileinputstream readimage(string path) throws ioexception { return new fileinputstream(new file(path)); } // 读取表中图片获取输出流 public static void readbin2image(inputstream in, string targetpath) { file file = new file(targetpath); string path = targetpath.substring(0, targetpath.lastindexof("/")); if (!file.exists()) { new file(path).mkdir(); } fileoutputstream fos = null; try { fos = new fileoutputstream(file); int len = 0; byte[] buf = new byte[1024]; while ((len = in.read(buf)) != -1) { fos.write(buf, 0, len); } fos.flush(); } catch (exception e) { e.printstacktrace(); } finally { if (null != fos) { try { fos.close(); } catch (ioexception e) { e.printstacktrace(); } } } } }
四、转码存储
package jdbcimgtest; import java.io.fileinputstream; import java.io.inputstream; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; /** * @author administrator 测试写入数据库以及从数据库中读取 */ public class imagedemo { // 将图片插入数据库 public static void readimage2db() { string path = "d:/eclipse/eclipseworkspace/testproject/img/mogen.jpg"; connection conn = null; preparedstatement ps = null; fileinputstream in = null; try { in = imageutil.readimage(path); conn = dbutil.getconn(); string sql = "insert into photo (id,name,photo)values(?,?,?)"; ps = conn.preparestatement(sql); ps.setint(1, 1); ps.setstring(2, "tom"); ps.setbinarystream(3, in, in.available()); int count = ps.executeupdate(); if (count > 0) { system.out.println("插入成功!"); } else { system.out.println("插入失败!"); } } catch (exception e) { e.printstacktrace(); } finally { dbutil.closeconn(conn); if (null != ps) { try { ps.close(); } catch (sqlexception e) { e.printstacktrace(); } } } } // 读取数据库中图片 public static void readdb2image() { string targetpath = "c:/users/jia/desktop/mogen.jpg"; connection conn = null; preparedstatement ps = null; resultset rs = null; try { conn = dbutil.getconn(); string sql = "select * from photo where id =?"; ps = conn.preparestatement(sql); ps.setint(1, 1); rs = ps.executequery(); while (rs.next()) { inputstream in = rs.getbinarystream("photo"); imageutil.readbin2image(in, targetpath); } } catch (exception e) { e.printstacktrace(); } finally { dbutil.closeconn(conn); if (rs != null) { try { rs.close(); } catch (sqlexception e) { e.printstacktrace(); } } if (ps != null) { try { ps.close(); } catch (sqlexception e) { e.printstacktrace(); } } } } //测试 public static void main(string[] args) { //readimage2db(); readdb2image(); } }