Hibernate在SqlServer中上传图像
1、sqlServer中photo表字段(int id,image image,varchar contentType)
2、html页面如下:
?3、uploadPhotoAction如下:
Integer photoId;try { FileInputStream in = new FileInputStream(photo); byte[] b = new byte[in.available()]; in.read(b); in.close(); Photo image = new Photo(); image.setContentType(uploadContentType); image.setImage(Hibernate.createBlob(b)); Session session = sessionFactory.getCurrentSession(); session.save(image);} catch (FileNotFoundException e) {e.printStackTrace();return "error";} catch (IOException e) {e.printStackTrace();return "error";}catch (RuntimeException re) {re.printStackTrace();throw re;}return "success";}?
?