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

实现向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())";

java 代码
  1. public void doInsertStaffPic(String loginname,String source_URL) {   
  2.   
  3.               Connection conn = null;   
  4.   
  5.               PreparedStatement pre = null;   
  6.               try {   
  7.   
  8.              // 进行数据库连接,这里我使用的是在STRUTS中配置的连接池,当然也可// 以自己通过JDBC直接连   
  9.                      conn = DBProcess.getConnection();                      
  10. //从图片源中获得图片对象并写到缓存中   
  11.   
  12.                      Image image = new ImageIcon(source_URL).getImage();   
  13.   
  14.                      BufferedImage bImage = new BufferedImage(image.getWidth(null),   
  15.   
  16.                                    image.getHeight(null), BufferedImage.TYPE_INT_RGB);   
  17.   
  18.                      Graphics bg = bImage.getGraphics();   
  19.   
  20.                      bg.drawImage(image, 00null);   
  21.   
  22.                      bg.dispose();                       
  23.   
  24. //将图片写入2进制的输出流 并放如到byte[] buf中   
  25.   
  26.                      ByteArrayOutputStream out = new ByteArrayOutputStream();   
  27.   
  28.                      ImageIO.write(bImage, "jpg", out);   
  29.   
  30.                      byte[] buf = out.toByteArray();                       
  31.   
  32.             //获得这个输出流并将他设置到BLOB中   
  33.                      ByteArrayInputStream inStream = new ByteArrayInputStream(buf);   
  34.                      pre = conn.prepareStatement(insertstaffpicquery);   
  35.                      pre.setString(1, loginname);   
  36.                      pre.setBinaryStream(2, inStream, inStream.available());   
  37.                      // 执行写如数据   
  38.   
  39. pre.executeUpdate();                     
  40.               } catch (Exception exc) {   
  41.                      exc.printStackTrace();   
  42.               }   
  43.               finally {   
  44.                      try {   
  45.                             pre.close();   
  46.                             conn.close();   
  47.                      } catch (SQLException e) {   
  48.                             e.printStackTrace();   
  49.                      }   
  50.               }   
  51.        }  

 

2 下代码实现从MYSQL中获取图片文件并写入本地文件系统:
…………………………

private final String writeoutquery = "insert into employeephoto (Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";

java 代码
  1. // retrive the picture data from database and write it to the local disk   
  2.   
  3.        public void doGetAndShowStaffPic(String loginname, String dir) {   
  4.         FileOutputStream output = null;   
  5.   
  6.         InputStream input = null;   
  7.               Connection conn = null;   
  8.               ResultSet rs = null;   
  9.               PreparedStatement pre = null;   
  10.               try {   
  11.   
  12.                      conn = DBProcess.getConnection();   
  13.                      pre = conn.prepareStatement(writeoutquery);   
  14.                      pre.setString(1, loginname);   
  15.                      rs = pre.executeQuery();   
  16.                      if (rs.next()) {   
  17.   
  18.                 // 从数据库获得2进制文件数据   
  19.                             Blob image = rs.getBlob("Binary_Photo");   
  20.   
  21.                             // setup the streams   
  22.                             Input = image.getBinaryStream();                     
  23.                             try {   
  24.   
  25.                     // 设置写出路径。   
  26.                                    output = new FileOutputStream(dir);   
  27.                             } catch (FileNotFoundException e1) {                                    
  28.                                    e1.printStackTrace();   
  29.                             }   
  30.                             // set read buffer size 注意不要设置的太小,要是太小,图片可能不完整   
  31.                             byte[] rb = new byte[1024000];   
  32.                             int ch = 0;   
  33.                             // process blob   
  34.                             try {   
  35.   
  36.                    // 写入本地文件系统   
  37.                                    while ((ch = input.read(rb)) != -1) {   
  38.                                           output.write(rb, 0, ch);                                            
  39.                                    }                                      
  40.                             } catch (IOException e) {                                     
  41.                                    e.printStackTrace();   
  42.                             }                              
  43.                             try {   
  44.                                    input.close();   
  45.                             } catch (IOException e) {                                     
  46.                                    e.printStackTrace();   
  47.                             }   
  48.                             try {   
  49.                                    output.close();   
  50.                             } catch (IOException e) {                           
  51.                                    e.printStackTrace();   
  52.                             }   
  53.                      }   
  54.               } catch (SQLException e) {   
  55.                      e.printStackTrace();   
  56.               }   
  57.               finally {   
  58.                      try {   
  59.                             rs.close();   
  60.                             pre.close();   
  61.                             conn.close();   
  62.                      } catch (SQLException e) {   
  63.                             e.printStackTrace();   
  64.                      }   
  65.               }   
  66.        }  


来源于javaResearch,作者 Jegg

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值