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

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

问题描述

我有PostgreSQL数据库,并且数据类型为'bytea'的列'image'。我无法修改列或数据库配置。 JPA带注释的POJO包含跟随映射

I have PostgreSQL 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=

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

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 ?

数据库 - PostgresSQL版本9.5.1

Database - PostgresSQL Version 9.5.1

驱动程序

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


推荐答案

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

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从PostgreSQL读取bytea图像数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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