共計 3578 個字符,預計需要花費 9 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
丸趣 TV 小編給大家分享一下將圖片添加到 mysql 中的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
將圖片添加到 mysql 中的方法:首先將數據庫存儲圖片的字段類型設置為 blob 二進制大對象類型;然后將圖片流轉化為二進制;最后將圖片插入數據庫即可。
正常的圖片儲存要么放進本地磁盤,要么就存進數據庫。存入本地很簡單,現在我在這里記下如何將圖片存進 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
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)
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;
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)
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;
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)
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;
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)
rs.close();
catch (SQLException e)
e.printStackTrace();
if (ps != null)
ps.close();
catch (SQLException e)
e.printStackTrace();
// 測試
public static void main(String[] args)
//readImage2DB();
readDB2Image();}
看完了這篇文章,相信你對將圖片添加到 mysql 中的方法有了一定的了解,想了解更多相關知識,歡迎關注丸趣 TV 行業資訊頻道,感謝各位的閱讀!
向 AI 問一下細節
丸趣 TV 網 – 提供最優質的資源集合!
正文完