一.简述
通过Java实现将图片存入MySQL数据库,并将其读取出来。需要用到字节流处理图片和JDBC连接数据库。
二.编程步骤
- 创建一张数据表用于存放数据库,这里我的数据库名为my_image_db,表名为photo,元素分为id (int),name(varchar),photo(blob)。这个表自行创建
- 通过JDBC连接MySQL数据库
- 封装读取图片的流
- 将图片插入数据库
- 将图片读取出来存入文件夹中
三.简单实例
代码中的图片路径,以及sql语句需根据你的实际情况更改
public class Main_10_20 {
public static void main(String[] args) throws Exception{
//System.out.println("开始!");
//ImageDemo.readImage2DB();//将图片插入数据库
//ImageDemo.readDB2Image();//读取图片
}
}
连接数据库
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Connection;
public class DBUtil {
public static final String Driver_Name="com.mysql.cj.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/my_image_db";//端口用于区分应用,3306是mysql的端口
public static final String USER = "root";
public static final String PASSWORD = "***";//连接数据库的密码
static { //静态块
try {
Class.forName(Driver_Name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn() throws SQLException{
Connection conn = DriverManager.getConnection(URL,USER,PASSWORD);
return conn;
}
public static void closeConn(Connection conn){
if(null!=conn){
try{
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
封装读取图片的流
import java.io.*;
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) {
//System.out.println(len);
fos.write(buf, 0, len);
}
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
实现图片的插入以及读取
import java.sql.*;
import java.io.*;
public class ImageDemo {
// 将图片插入数据库
public static void readImage2DB() {
System.out.println("将图片插入数据库");
String path = "/Users/huangjw/Documents/read.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");
System.out.println(in.available());
ps.setBinaryStream(3, in, in.available());
int count = ps.executeUpdate();//执行sql
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 = "/Users/huangjw/Desktop/other/read.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();
}
}
}
}
}