java - 如何在 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;
解决方案
我认为这可能与以下内容重复: Large Objects may not be used in auto-commit mode
看看它是否能解决你的问题。
推荐阅读
- jquery - 带有有效数据的数据表警告
- sql - 如何根据列的值获取sql server中的行
- python - 如何从大文本文件中的随机位置进行 python 正则表达式搜索
- android - 一起使用 firestore 和 firebase auth/core 的问题
- c# - 如何在没有 Excel 的情况下使用 ExcelDNA 从 C# 开发的 XLL
- bigrquery - R 无法通过 bigrquery 包导入数据 - !self$finished 不是 TRUE
- angular - ngFor 不显示来自可观察事件源的数据
- excel-formula - 特定日历月的所有对应值的总和以及截至上个月的所有对应值的总和?
- r - 在 R 中总结组均值时如何创建有条件的新组
- wordpress - 如何知道wordpress中安装了哪些插件