我希望您将图像以BLOB类型存储在表中(如果不尝试这样做,因为这是最佳做法)。让我们假设您有一个
Person类,其中包含
image存储在数据库中的人员的。如果要对此进行映射,只需在个人POJO中添加保存图像的属性。
@Column(name="image")@Blobprivate Blob image;
显示时,将其转换为a
byte[]并显示。
private byte[] toByteArray(Blob fromImageBlob) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { return toByteArrayImpl(fromImageBlob, baos); } catch (Exception e) { } return null; }private byte[] toByteArrayImpl(Blob fromImageBlob, ByteArrayOutputStream baos) throws SQLException, IOException { byte buf[] = new byte[4000]; int dataSize; InputStream is = fromImageBlob.getBinaryStream(); try { while((dataSize = is.read(buf)) != -1) { baos.write(buf, 0, dataSize); } } finally { if(is != null) { is.close(); } } return baos.toByteArray(); }
您可以查看以下示例以了解更多信息。
- http://i-proving.com/2006/08/23/blobs-and-hibernate
- http://snehaprashant.blogspot.com/2008/08/how-to-store-and-retrieve-blob-object.html
- http://viralpatel.net/blogs/2011/01/tutorial-save-get-blob-object-spring-3-mvc-hibernate.html
如您所见,有多种方法可以做到这一点。选择一个适合您的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)