实现向MYSQL数据库中存储或提取图片文件
发表于:2025-11-09 作者:千家信息网编辑
千家信息网最后更新 2025年11月09日,一些情况下,需要向数据库中存储一些2进制文件,比如图片文件等,这时候,向数据库存储数据不同于普通的字符串存储,我们需要对这个2进制文件使用JAVA处理2进制流的API进行处理,然后再进行存储。我们需要
千家信息网最后更新 2025年11月09日实现向MYSQL数据库中存储或提取图片文件一些情况下,需要向数据库中存储一些2进制文件,比如图片文件等,这时候,向数据库存储数据不同于普通的字符串存储,我们需要对这个2进制文件使用JAVA处理2进制流的API进行处理,然后再进行存储。我们需要进行以下步骤来实现:
向数据库中存储文件的时候,一样使用标准SQL语句,如: insert into database (column1, column2,..) values(v1,v2,…);注意的是,要在建立存放2进制文件的TABLE时,存放的字段要使用BLOB类型,而不是普通的VARCHAR等。BLOB是专门存储2进制文件的类型,他还有大小之分,比如mediablob,logblob等,以存储大小不同的2进制文件,一般的图形文件使用mediablob足以了。
1 见以下代码实现向MYSQL中储存图片文件:
…………………………
private final String insertquery = "insert into employeephoto (Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";
public void doInsertStaffPic(String loginname,String source_URL) {
Connection conn = null;
PreparedStatement pre = null;
try {
// 进行数据库连接,这里我使用的是在STRUTS中配置的连接池,当然也可// 以自己通过JDBC直接连
conn = DBProcess.getConnection();
//从图片源中获得图片对象并写到缓存中
Image image = new ImageIcon(source_URL).getImage();
BufferedImage bImage = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics bg = bImage.getGraphics();
bg.drawImage(image, 0, 0, null);
bg.dispose();
//将图片写入2进制的输出流 并放如到byte[] buf中
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", out);
byte[] buf = out.toByteArray();
//获得这个输出流并将他设置到BLOB中
ByteArrayInputStream inStream = new ByteArrayInputStream(buf);
pre = conn.prepareStatement(insertstaffpicquery);
pre.setString(1, loginname);
pre.setBinaryStream(2, inStream, inStream.available());
// 执行写如数据
pre.executeUpdate();
} catch (Exception exc) {
exc.printStackTrace();
}
finally {
try {
pre.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2 下代码实现从MYSQL中获取图片文件并写入本地文件系统:
…………………………
private final String writeoutquery = "insert into employeephoto (Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";
// retrive the picture data from database and write it to the local disk
public void doGetAndShowStaffPic(String loginname, String dir) {
FileOutputStream output = null;
InputStream input = null;
Connection conn = null;
ResultSet rs = null;
PreparedStatement pre = null;
try {
conn = DBProcess.getConnection();
pre = conn.prepareStatement(writeoutquery);
pre.setString(1, loginname);
rs = pre.executeQuery();
if (rs.next()) {
// 从数据库获得2进制文件数据
Blob image = rs.getBlob("Binary_Photo");
// setup the streams
Input = image.getBinaryStream();
try {
// 设置写出路径。
output = new FileOutputStream(dir);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
// set read buffer size 注意不要设置的太小,要是太小,图片可能不完整
byte[] rb = new byte[1024000];
int ch = 0;
// process blob
try {
// 写入本地文件系统
while ((ch = input.read(rb)) != -1) {
output.write(rb, 0, ch);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
try {
rs.close();
pre.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
[@more@]
向数据库中存储文件的时候,一样使用标准SQL语句,如: insert into database (column1, column2,..) values(v1,v2,…);注意的是,要在建立存放2进制文件的TABLE时,存放的字段要使用BLOB类型,而不是普通的VARCHAR等。BLOB是专门存储2进制文件的类型,他还有大小之分,比如mediablob,logblob等,以存储大小不同的2进制文件,一般的图形文件使用mediablob足以了。
1 见以下代码实现向MYSQL中储存图片文件:
…………………………
private final String insertquery = "insert into employeephoto (Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";
public void doInsertStaffPic(String loginname,String source_URL) {
Connection conn = null;
PreparedStatement pre = null;
try {
// 进行数据库连接,这里我使用的是在STRUTS中配置的连接池,当然也可// 以自己通过JDBC直接连
conn = DBProcess.getConnection();
//从图片源中获得图片对象并写到缓存中
Image image = new ImageIcon(source_URL).getImage();
BufferedImage bImage = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics bg = bImage.getGraphics();
bg.drawImage(image, 0, 0, null);
bg.dispose();
//将图片写入2进制的输出流 并放如到byte[] buf中
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", out);
byte[] buf = out.toByteArray();
//获得这个输出流并将他设置到BLOB中
ByteArrayInputStream inStream = new ByteArrayInputStream(buf);
pre = conn.prepareStatement(insertstaffpicquery);
pre.setString(1, loginname);
pre.setBinaryStream(2, inStream, inStream.available());
// 执行写如数据
pre.executeUpdate();
} catch (Exception exc) {
exc.printStackTrace();
}
finally {
try {
pre.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2 下代码实现从MYSQL中获取图片文件并写入本地文件系统:
…………………………
private final String writeoutquery = "insert into employeephoto (Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";
// retrive the picture data from database and write it to the local disk
public void doGetAndShowStaffPic(String loginname, String dir) {
FileOutputStream output = null;
InputStream input = null;
Connection conn = null;
ResultSet rs = null;
PreparedStatement pre = null;
try {
conn = DBProcess.getConnection();
pre = conn.prepareStatement(writeoutquery);
pre.setString(1, loginname);
rs = pre.executeQuery();
if (rs.next()) {
// 从数据库获得2进制文件数据
Blob image = rs.getBlob("Binary_Photo");
// setup the streams
Input = image.getBinaryStream();
try {
// 设置写出路径。
output = new FileOutputStream(dir);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
// set read buffer size 注意不要设置的太小,要是太小,图片可能不完整
byte[] rb = new byte[1024000];
int ch = 0;
// process blob
try {
// 写入本地文件系统
while ((ch = input.read(rb)) != -1) {
output.write(rb, 0, ch);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
try {
rs.close();
pre.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
[@more@]
文件
数据
进制
图片
存储
数据库
普通
代码
大小
类型
系统
处理
输出
不同
图形
字段
字符
字符串
对象
情况
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
计算机网络安全结课总结报告
数据库查看以什么开头的总数据量
苏州阿里云时空数据库
服务器集中管理 免费
php 中页面查询数据库
国开行软件开发中心
融创互联网科技
fik和matlab服务器
软件开发必修课程
税务系统网络安全措施
外贸服务器哪个牌子好
我的世界服务器管理员圈地
网络安全工程师电脑增高
政务云网络安全合规性
小红书老是显示未连接到服务器
国企网络安全员
税控服务器是什么
数据库附加坏了
丰台区加工软件开发
网络安全模式分为
数据库模式是表
数据库触发器利弊
网络安全技术专业能进54所吗
雪人服务器
华为网络技术人员上门服务
数据库删除失败
我的世界服务器计分板隐藏功能
加强网络安全奖惩
怎么搭建自己的服务器网络配置
wr703无线打印服务器设置