How to read bytea image data from PostgreSQL with JPA?

后端 未结 5 947
离开以前
离开以前 2020-12-31 08:56

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

5条回答
  •  自闭症患者
    2020-12-31 09:40

    ImageEntity

    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;
        }
    }
    

    ImageRepository

    package com.example;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public interface ImageRepository extends JpaRepository {
    }
    

    Test

    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]);
            }
    
        }
    
    }
    

    It works against Postgres 9.5.0-1 with 9.4.1207.jre7 jdbc driver.

提交回复
热议问题