如何使用JPA从PostgreSQL读取bytea图像数据?

Bhu*_*han 7 java postgresql hibernate jpa bytea

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

@Column(name="image")
private byte[] image;
Run Code Online (Sandbox Code Playgroud)

返回的数据采用以下格式(这只是一个示例)

WF5ClN6RlpLZ0hJTUdNQ1FJWmkwcFVGSUdNQ0lDWUE5TUEvanRFeElwK2x0M2tBQUFBQVNVVk9SSzVDWUlJPQo=
Run Code Online (Sandbox Code Playgroud)

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

数据库 - PostgresSQL版本9.5.1

司机

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1205-jdbc41</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

Mik*_*neu 6

图像实体

package com.example;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class ImageEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

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

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }
}
Run Code Online (Sandbox Code Playgroud)

图像库

package com.example;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ImageRepository extends JpaRepository<ImageEntity, Long> {
}
Run Code Online (Sandbox Code Playgroud)

测试

package com.example;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import junit.framework.TestCase;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class)
public class ImageDaoTest {

    @Resource
    private ImageRepository imageRepository;

    @Test
    public void testImage() throws IOException {

        // Read an image from disk. Assume test.png exists
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try (InputStream in = getClass().getResourceAsStream("test.png")) {
            int length;
            byte[] buffer = new byte[1024];
            while ((length = in.read(buffer)) != -1) out.write(buffer, 0, length);
        }

        byte[] image = out.toByteArray();

        // Store image to DB
        ImageEntity imageEntiry = new ImageEntity();
        imageEntiry.setImage(image);
        long imageEntiryId = imageRepository.save(imageEntiry).getId();

        // Retrieve image from DB
        ImageEntity resultImageEntiry = imageRepository.findOne(imageEntiryId);
        byte[] resultImage = resultImageEntiry.getImage();

        // Compare retrieved image with source image by byte to byte comparison
        for (int i = 0; i < resultImage.length; i++) {
            TestCase.assertEquals(image[i], resultImage[i]);
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

它适用于带有 9.4.1207.jre7 jdbc 驱动程序的 Postgres 9.5.0-1。


小智 5

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

有关解码的更多信息,请查看此处


jos*_*van 5

尝试用注释您的实体 @Lob

@Lob
@Column(name="image")
private byte[] image;
Run Code Online (Sandbox Code Playgroud)

如果您正在使用休眠实现,则也可以@Type(type="org.hibernate.type.BinaryType")在列中添加。

@Lob
@Column(name="image")
@Type(type="org.hibernate.type.BinaryType")
private byte[] image;
Run Code Online (Sandbox Code Playgroud)