Blob存储读取图片

来源:互联网 发布:python numpy安装 编辑:程序博客网 时间:2024/06/10 08:57
Java代码 复制代码 收藏代码
  1. package com.jdbctest.clobtest;   
  2. import java.io.BufferedInputStream;   
  3. import java.io.File;   
  4. import java.io.FileInputStream;   
  5. import java.io.FileNotFoundException;   
  6. import java.io.FileOutputStream;   
  7. import java.io.IOException;   
  8. import java.io.InputStream;   
  9. import java.io.OutputStream;   
  10. import java.sql.*;   
  11.   
  12. import com.jdbctest.Util.JDBCUtil;   
  13.   
  14. public class BlobTest {   
  15.         public static void main(String args[]){   
  16.             Connection conn = null;   
  17.             Statement stmt = null;   
  18.             PreparedStatement ps = null;   
  19.             ResultSet rs = null;   
  20.                
  21.             //获取数据库对象   
  22.             conn = JDBCUtil.getConnection();   
  23.             //表存在则删除它   
  24.             String sql = "DROP TABLE IF EXISTS blobtest";   
  25.                
  26.             try {   
  27.                 stmt = conn.createStatement();   
  28.                 System.out.println(sql);   
  29.                 stmt.executeUpdate(sql);   
  30.                 //创建表   
  31.                 sql = "CREATE TABLE blobtest(" +    
  32.                 "b_id INT NOT NULL AUTO_INCREMENT,"+   
  33.                 "b_title VARCHAR(50),"+   
  34.                 "b_text LongBlob,"+   
  35.                 "PRIMARY KEY(b_id)"+   
  36.                 ")";   
  37.             System.out.println(sql);   
  38.             stmt.executeUpdate(sql);   
  39.             System.out.println("创建数据表成功!");   
  40.                
  41.             sql="INSERT INTO blobtest(b_title,b_text)VALUES(?,?)";   
  42.             ps = conn.prepareStatement(sql);   
  43.             //插入图片   
  44.             //File file = new File("F:\\image\\yahoo.jpg");  
  45.             File file = new File("d:\\wuyu\\image\\sea.jpg");   
  46.             InputStream inputStream = new FileInputStream(file);   
  47.             //BufferedInputStream bis=new BufferedInputStream(inputStream);  
  48.                
  49.             //以前的时候直接使用setBinaryStream就可以保存图片,但  
  50.             //数据库的字符集换成 gbk后就不能用了,一直提示参数类型不匹配,在网上查了一下,  
  51.             //直接向数据库中存byte可解决   
  52.             //ps.setString(1, file.getName());  
  53.             //ps.setBinaryStream(2,inputStream,file.length());  
  54.             //ps.executeUpdate();   
  55.             try {   
  56.                 ps.setString(1, file.getName());   
  57.                 //新建一byte数组   
  58.                 byte[] buf=new byte[inputStream.available()];   
  59.                 //将文件读入到byte[]中   
  60.                 inputStream.read(buf);   
  61.                 ps.setBytes(2, buf);   
  62.                 ps.executeUpdate();   
  63.                 System.out.println("插入成功!");   
  64.             } catch (IOException e1) {   
  65.                 System.out.println("保存图片到数据库成功!");   
  66.                 e1.printStackTrace();   
  67.             }   
  68.                
  69.                
  70.                
  71.             //读取数据   
  72.                
  73.             sql = "SELECT b_title,b_text FROM blobtest";   
  74.             ps = conn.prepareStatement(sql);   
  75.             rs = ps.executeQuery();   
  76.             while(rs.next()){   
  77.                 System.out.println("图片名: "+rs.getString("b_title"));   
  78.                 //System.out.println("电影名: "+rs.getString("b_title"));  
  79.                 Blob blob = rs.getBlob("b_text");   
  80.                 File file2 = new File("d:\\wuyu\\sea.jpg");   
  81.                 OutputStream outputStream = new FileOutputStream(file2);   
  82.                 try {   
  83.                     outputStream.write(blob.getBytes(1,(int)blob.length()));   
  84.                 } catch (IOException e) {   
  85.                     e.printStackTrace();   
  86.                 }   
  87.                 //打印出来的为对象   
  88.                 System.out.println("图片内容: "+ blob.getBinaryStream());   
  89.                    
  90.             }   
  91.             } catch (SQLException e) {   
  92.                 e.printStackTrace();   
  93.             } catch (FileNotFoundException e) {   
  94.                 e.printStackTrace();   
  95.             }finally{   
  96.                 JDBCUtil.close(rs, stmt, conn);   
  97.             }   
  98.                
  99.         }   
  100.   
  101. }  
原创粉丝点击