hibernate对大数据资源的处理操作(三)

2014-11-24 07:31:01 · 作者: · 浏览: 23
try { // 创建文件的输入流,将文件加载到流中 FileInputStream fis = new FileInputStream(file); // 创建blob大数据对象|||||在4之后要用这样的方式获取 Blob blob = Hibernate.getLobCreator(HiberUtil.openSession()) .createBlob(fis, file.length()); //将大数据存储到 image.setImage(blob); imageService.saveObject(image); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void getImage() throws SQLException { Image image = (Image) imageService.getObject(Image.class, 1); // 判断得到得数据是否为空 if (image != null) { InputStream is = image.getImage().getBinaryStream(); File file = new File("D:\\a.jpg"); try { FileOutputStream fos = new FileOutputStream(file); byte buffer[] = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); } fos.close(); is.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }