將圖片添加到mysql中的方法:首先將數(shù)據(jù)庫存儲圖片的字段類型設(shè)置為blob二進(jìn)制大對象類型;然后將圖片流轉(zhuǎn)化為二進(jìn)制;最后將圖片插入數(shù)據(jù)庫即可。
推薦:《mysql視頻教程》
正常的圖片儲存要么放進(jìn)本地磁盤,要么就存進(jìn)數(shù)據(jù)庫。存入本地很簡單,現(xiàn)在我在這里記下如何將圖片存進(jìn)mysql數(shù)據(jù)庫?
如果要圖片存進(jìn)數(shù)據(jù)庫? 要將圖片轉(zhuǎn)化成二進(jìn)制。
1.數(shù)據(jù)庫存儲圖片的字段類型要為blob二進(jìn)制大對象類型
2.將圖片流轉(zhuǎn)化為二進(jìn)制
下面放上代碼實(shí)例
一、數(shù)據(jù)庫
CREATE?TABLE?`photo`?( ??`id`?int(11)?NOT?NULL, ??`name`?varchar(255)?DEFAULT?NULL, ??`photo`?blob, ??PRIMARY?KEY?(`id`) )?ENGINE=InnoDB?DEFAULT?CHARSET=utf8;
二、數(shù)據(jù)庫鏈接
/** ?*? ?*/ package?JdbcImgTest; import?java.sql.Connection; import?java.sql.DriverManager; import?java.sql.SQLException; /** ?*?@author?Administrator ?*? ?*/ public?class?DBUtil { ????//?定義數(shù)據(jù)庫連接參數(shù) ????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"; ???? ????//?注冊數(shù)據(jù)庫驅(qū)動 ????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); ????} ???? ????//?關(guān)閉連接 ????public?static?void?closeConn(Connection?conn) ????{ ????????if?(null?!=?conn) ????????{ ????????????try ????????????{ ????????????????conn.close(); ????????????} ????????????catch?(SQLException?e) ????????????{ ????????????????System.out.println("關(guān)閉連接失敗!"); ????????????????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(); ????????????????} ????????????} ????????} ????} }
四、轉(zhuǎn)碼存儲
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?測試寫入數(shù)據(jù)庫以及從數(shù)據(jù)庫中讀取 ?*/ public?class?ImageDemo { ???? ????//?將圖片插入數(shù)據(jù)庫 ????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(); ????????????????} ????????????} ????????} ???????? ????} ???? ????//?讀取數(shù)據(jù)庫中圖片 ????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(); ????} }
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END