图片的读取
我读取出来的图片怎么是空的?
我读取出来的图片怎么是空的?
2016-04-08
@Test public void testWriteBlob()throws Exception{ Students s=new Students(1,"jack","男",new Date(),"广州"); //先获得照片文件 File f=new File("d:"+File.separator+"games_control.jpg"); //获取文件的输入流 InputStream input=new FileInputStream(f); //创建一个Blob对象 Blob image=Hibernate.getLobCreator(session).createBlob(input,input.available()); //设置图片属性 s.setPicture(image); //保存学生 session.save(s); } @Test public void testReadBlob()throws Exception{ Students s=(Students)session.get(Students.class, 1); //获取Blob对象 Blob image=s.getPicture(); //获取照片的输入流 InputStream input=image.getBinaryStream(); //创建输出流 File f=new File("d:"+File.separator+"dest.jpg"); //获取输出流 OutputStream output=new FileOutputStream(f); //创建缓冲区 byte[] buff=new byte[input.available()]; input.read(buff); output.write(buff); input.close(); output.close(); }
举报