转载自博客园:小学森也要编程 数据库存取图片(二进制方式)

如何从数据库读取存入的图片,即Blob(二进制)数据。
先从数据库读取对象。
再从获取的对象中得到blob对象。
通过blob对象的getBinaryStream()方法获取input输出流。
之后通过输出流,写文件。

①在创建表结构时添加:
  private Blob picture;//添加照片属性
 生成get,set方法,重写构造函数,在配置文件中添加:
  <property name="picture" type="java.sql.Blob">
    <column name="picture" />
  </property>
*引包用import java.sql.Blob;千万不能引错

②编写测试类,将照片写入数据库,代码如下:

  @Test
  public void testWriteBlob() throws Exception{
    //生成用户对象
    User u = new User(2,"李四","男");
    //获得照片文件
    File f = new File("d:"+File.separator+"123.JPG");
    //获得照片文件的输入流
    InputStream input = new FileInputStream(f);
    //创建一个Blob对象
    Blob image = Hibernate.getLobCreator(session).createBlob(input, input.available());
    //设置照片属性
    u.setPicture(image);
    //保存用户
    session.save(u);
  }

 ③将数据库中照片读出到D盘,文件名为abc:

  @Test
  public void testReadBlob() throws Exception{
    User u = (User)session.get(User.class, 2);
    //获得Blob对象
    Blob image = u.getPicture();
    //获得照片的输入流
    InputStream input = image.getBinaryStream();
    //创建输出流
    File f = new File("d:"+File.separator+"abc.jpg");
    //获得输出流
    OutputStream output = new FileOutputStream(f);
    //创建缓冲区
    byte[] buff = new byte[input.available()];
    input.read(buff);
    output.write(buff);
    input.close();
    output.close();
  }

请教一下-数据库存取图片的问题

09-29

我虽然查到了不少说明数据库存取的文章,但都有些问题,以下是我的代码,不知哪里有毛病。数据库中name 为文件名,pic为二进制数据,len为文件长度rncommand1为把图片存入数据库中,command2为从数据库中取数据生成图片rn用command1存入的文件用其它方法读出后色彩有点错位,而用command2根本读不出来,读循环一半的时候出错,说类型不匹配。rn环境为ado2.0,oracle数据库rnrnrnOption ExplicitrnDim rst As New ADODB.RecordsetrnDim cn As New ADODB.ConnectionrnPrivate Const max = 1000rnPrivate Sub Command1_Click()rnDim fp As LongrnDim flen As LongrnDim lob() As ByternDim num As IntegerrnDim leftlob As LongrnDim i As Integerrn dlgFile.ShowOpenrn If dlgFile.FileName = "" Thenrn Exit Subrn End Ifrn fp = FreeFilern Open dlgFile.FileName For Binary As #fprn flen = LOF(fp)rn num = flen \ maxrn leftlob = flen Mod maxrn rn rn rst.Open "select * from pic", cn, adOpenKeyset, adLockBatchOptimisticrn rst.AddNewrn rst!Name = dlgFile.FileNamern rst!Len = flenrn If leftlob > 0 Thenrn ReDim lob(leftlob) As Bytern Get #fp, , lob()rn rst!pic.AppendChunk lob()rn End Ifrn ReDim lob(max) As Bytern For i = 1 To numrn Get #fp, , lob()rn rst!pic.AppendChunk lob()rn Next irn rn Close fprn rst.UpdateBatchrn rst.Closern rnEnd SubrnrnPrivate Sub Command2_Click()rnDim fp As LongrnDim flen As LongrnDim num As IntegerrnDim i As IntegerrnDim lob() As ByternDim v As VariantrnDim leftlob As LongrnDim lngoffset As Longrn rst.Open "select * from pic", cn, adOpenKeyset, adLockReadOnlyrn flen = rst!Lenrn num = flen \ maxrn leftlob = flen Mod maxrn fp = FreeFilern Open App.Path & "\test.jpg" For Binary Access Write As #fprn ReDim lob(leftlob)rn lob = rst!pic.GetChunk(leftlob)rn Put #fp, , lobrn ReDim lob(max)rn For i = 1 To numrn lob() = rst!pic.GetChunk(max)rn Put #fp, , lob()rn Next irn ReDim lob(leftlob)rn lob() = rst!pic.GetChunk(max)rn Put #fp, , lob()rn Close fprn rst.Closern rnEnd Subrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试