Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring Boot应用程序(Hibernate+;JPA)中从Postgresql提取图像(lob)?_Java_Postgresql_Spring Boot_Hibernate_Spring Data Jpa - Fatal编程技术网

Java 如何在Spring Boot应用程序(Hibernate+;JPA)中从Postgresql提取图像(lob)?

Java 如何在Spring Boot应用程序(Hibernate+;JPA)中从Postgresql提取图像(lob)?,java,postgresql,spring-boot,hibernate,spring-data-jpa,Java,Postgresql,Spring Boot,Hibernate,Spring Data Jpa,我有一个逻辑,它通过java中的byte[]从f-end获取图像,压缩它们,然后将它们存储在Postgresql db中的一个列中,该列使用数据lob定义 服务: public CourseDto createNewCourse(CourseDto newCourseDto) throws SQLException { Courses course = courseRepositoryDao.findByCourseName(newCourseDto.getCourseName())

我有一个逻辑,它通过java中的byte[]从f-end获取图像,压缩它们,然后将它们存储在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();
    }

我认为这可能是重复的:


检查它是否解决了您的问题。

它无法解决问题。我对@Transactional也做了同样的操作,但它只有在我将ddl设置为“createdrop”时才起作用。当我将ddl设置为“更新”时,我的邮递员请求处于挂起状态,等待响应而不返回anithing。在服务方法上使用@Transactional时,我能够接收该对象,但只有当NN ddl auto设置为“create drop”时,它才起作用。如果我将ddl auto更改为“更新”,它将不与邮递员一起工作。
@Column(name = "picByte", length = 4000)
@Lob
private byte[] logo;