十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
小編給大家分享一下將圖片添加到MySQL中的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供加查網(wǎng)站建設(shè)、加查做網(wǎng)站、加查網(wǎng)站設(shè)計、加查網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、加查企業(yè)網(wǎng)站模板建站服務(wù),10余年加查做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
將圖片添加到mysql中的方法:首先將數(shù)據(jù)庫存儲圖片的字段類型設(shè)置為blob二進制大對象類型;然后將圖片流轉(zhuǎn)化為二進制;最后將圖片插入數(shù)據(jù)庫即可。
正常的圖片儲存要么放進本地磁盤,要么就存進數(shù)據(jù)庫。存入本地很簡單,現(xiàn)在我在這里記下如何將圖片存進mysql數(shù)據(jù)庫
如果要圖片存進數(shù)據(jù)庫 要將圖片轉(zhuǎn)化成二進制。
1.數(shù)據(jù)庫存儲圖片的字段類型要為blob二進制大對象類型
2.將圖片流轉(zhuǎn)化為二進制
下面放上代碼實例
一、數(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();
}
}看完了這篇文章,相信你對將圖片添加到mysql中的方法有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!