如何用JPA从postgresSQL读取bytea图像数据? [英] How to read bytea image data from postgresSQL with JPA?

查看:321
本文介绍了如何用JPA从postgresSQL读取bytea图像数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有postgresSQL数据库,并且有'bytea'数据类型的'image'列。我无法修改列或数据库配置。 JPA注释的POJO包含了followign映射



private byte [] image; @Column(name =image)
private byte [] image;

返回的数据格式如下(仅为示例)

  WF5ClN6RlpLZ0hJTUdNQ1FJWmkwcFVGSUdNQ0lDWUE5TUEvanRFeElwK2x0M2tBQUFBQVNVVk9SSzVDWUlJPQo = 

当我将这些数据写入文件(.jpeg),照片浏览器显示这是损坏的文件。我也明白,实际图像字节数据看起来不同于上面的示例。我读过一些博客,其中提到PostgreSQL将byteade数据应用于十六进制转换。如何使用或不使用JPA将其恢复为原始数据?

数据库 - PostgresSQL 9.5.1版本



驱动程序

 <依赖性> 
< groupId> org.postgresql< / groupId>
< artifactId> postgresql< / artifactId>
< version> 9.4-1205-jdbc41< / version>
< /依赖关系>


解决方案

返回的数据看起来好像是base64编码的。您必须在写入文件之前将其解码为二进制数据。



有关解码外观的更多信息这里


I have postgresSQL database and there is column 'image' with datatype 'bytea'. I cannot modify columns or database configurations. JPA annotated POJO contains followign mapping

@Column(name="image")
private byte[] image;

The returned data is in following format (this is just a sample)

WF5ClN6RlpLZ0hJTUdNQ1FJWmkwcFVGSUdNQ0lDWUE5TUEvanRFeElwK2x0M2tBQUFBQVNVVk9SSzVDWUlJPQo=

When I write this data to file (.jpeg) photo viewer says "this is corrupted file". I also understand that actual image byte data looks different from above sample. I read some blogs which mentioned that PostgreSQL applies hexadecimal conversion to bytea data. How to restore it to original data with or without JPA ?

Database - PostgresSQL Version 9.5.1

Driver

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1205-jdbc41</version>
</dependency>

解决方案

The returned Data looks as if it is base64 encoded. You have to decode it back to binary data before writing to file.

For further information to decode look here

这篇关于如何用JPA从postgresSQL读取bytea图像数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆