加入收藏 | 设为首页 | 会员中心 | 我要投稿 武汉站长网 (https://www.027zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

实现向MYSQL数据库中存储或提取图片文件

发布时间:2022-03-30 13:10:11 所属栏目:MySql教程 来源:互联网
导读:一些情况下,需要向数据库中存储一些2进制文件,比如图片文件等,这时候,向数据库存储数据不同于普通的字符串存储,我们需要对这个2进制文件使用JAVA处理2进制流的API进行处理,然后再进行存储。我们需要进行以下步骤来实现: 向数据库中存储文件的时候,
       一些情况下,需要向数据库中存储一些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();

(编辑:武汉站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读