首页 > 解决方案 > 如何在 Spring Boot 应用程序(Hibernate + JPA)中从 Postgresql 中提取图像(lob)?

问题描述

我有一个逻辑,通过 java 中的 byte[] 从 f 端获取图像,压缩它们,然后存储在 Postgresql db 中用数据 lob 定义的列中。

服务:

 public CourseDto createNewCourse(CourseDto newCourseDto) throws SQLException {

    Courses course = courseRepositoryDao.findByCourseName(newCourseDto.getCourseName());
    if (course == null) {

        course = new Courses()
                .setCourseName(newCourseDto.getCourseName())
                .setCourseDescription(newCourseDto.getCourseDescription())
                .setCoursePrice(newCourseDto.getCoursePrice())
                .setIsCourseFree(newCourseDto.getIsCourseFree())
                .setIsCourseActive(newCourseDto.getIsCourseActive())
                .setLogo(compressZLib(newCourseDto.getLogo()));

        ;
        return CourseMapper.toUserDtoFreeCourses(courseRepositoryDao.save(course));
    }
    throw exception(EntityType.NEWCOURSE, ExceptionType.DUPLICATE_ENTITY, newCourseDto.getCourseName());
}

// compress the image bytes before storing it in the database
public static byte[] compressZLib(byte[] data) {
    Deflater deflater = new Deflater();
    deflater.setInput(data);
    deflater.finish();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    try {
        outputStream.close();
    } catch (IOException e) {
    }
    System.out.println("Compressed Image Byte Size - " + outputStream.toByteArray().length);

    return outputStream.toByteArray();
}

我尝试检索图像:

    public List<CourseDto> getCoureses() {

        List<Courses> courses = courseRepositoryDao.findAllByIsCourseFreeAndIsCourseActive(true, true);
        List<CourseDto> coursesNameDto = courses
                .stream()
                .peek(i -> i.setLogo(decompressZLib(i.getLogo())))
                .map(course -> modelMapper.map(CourseMapper.toUserDtoFreeCourses(course), CourseDto.class)).collect(Collectors.toList());
        System.out.println("**************************" + coursesNameDto + "**********************");
        return coursesNameDto;
    }
    // uncompress the image bytes before returning it to the angular application
    public static byte[] decompressZLib(byte[] data) {
        Inflater inflater = new Inflater();
        inflater.setInput(data);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        try {
            while (!inflater.finished()) {
                int count = inflater.inflate(buffer);
                outputStream.write(buffer, 0, count);
            }
            outputStream.close();
        } catch (IOException ioe) {
        } catch (DataFormatException e) {
        }
        return outputStream.toByteArray();
    }

但我有这个错误:“对象可能不能在自动提交模式下使用”

我的实体类在徽标上有这个字段:

@Column(name = "picByte", length = 4000)
@Lob
private byte[] logo;

标签: javapostgresqlspring-boothibernatespring-data-jpa

解决方案


我认为这可能与以下内容重复: Large Objects may not be used in auto-commit mode

看看它是否能解决你的问题。


推荐阅读