在BLOB类型的数据库中存储图像

时间:2020-05-17 07:32:45

标签: spring postgresql spring-boot file blob

我有一个问题。如何将文件保存在数据库中?我将Spring Boot用于后端语言和PostgreSQL数据库。我有一个用户实体,并且有“头像”字段:

@Lob
@Column(name = "avatar", columnDefinition="BLOB")
private byte[] avatar;

@Lob
@Basic(fetch = FetchType.LAZY)
public byte[] getAvatar() {
   return avatar;
}
public void setAvatar(byte[] avatar) {
   this.avatar = avatar;
}

当我第一次注册用户时,化身为null,并且当我将用户保存到数据库时,我收到错误消息(InvalidDataAccessResourceUsageException)和以下消息:
错误:列“ avatar”的类型为jsonb,但表达式的类型为oid
提示:您将需要重写或强制转换表达式。 位置:173

我该怎么办才能解决这个问题?
Spring如何解决此问题并将文件保存在DB中?
最佳做法是什么?

1 个答案:

答案 0 :(得分:0)

您应该看看社区项目Spring Content。该项目为您提供了类似于Spring Data的API和内容开发方法。 Spring Data是针对非结构化数据(文档,图像,视频等)的结构化数据。您可以添加以下内容:-

pom.xml(也提供Spring Boot启动器)

   <!-- Java API -->
   <dependency>          
      <groupId>com.github.paulcwarren</groupId>
      <artifactId>spring-content-jpa</artifactId>
      <version>1.0.0.M10</version>
   </dependency>
   <!-- REST API -->
   <dependency>
      <groupId>com.github.paulcwarren</groupId>
      <artifactId>spring-content-rest</artifactId>
      <version>1.0.0.M10</version>
   </dependency>

配置

@Configuration
// enables Java API 
@EnableJpaStores
// enables REST API 
@Import("org.springframework.content.rest.config.RestConfiguration.class") 
public class ContentConfig {

   // specify the resource specific to your database
   @Value("/org/springframework/content/jpa/schema-drop-postgresql.sql")
   private ClasspathResource dropBlobTables;

   // specify the resource specific to your database
   @Value("/org/springframework/content/jpa/schema-postgresql.sql")
   private ClasspathResource createBlobTables;

   @Bean
   DataSourceInitializer datasourceInitializer() {
     ResourceDatabasePopulator databasePopulator =
            new ResourceDatabasePopulator();

     databasePopulator.addScript(dropBlobTables);
     databasePopulator.addScript(createBlobTables);
     databasePopulator.setIgnoreFailedDrops(true);

     DataSourceInitializer initializer = new DataSourceInitializer();
     initializer.setDataSource(dataSource());
     initializer.setDatabasePopulator(databasePopulator);

     return initializer;
   }
}

注意:如果您使用相关的Spring Boot启动器,则不需要此配置。

要关联内容,请将Spring Content批注添加到您的User实体。

User.java

@Entity
public class User {

   ...

   // replace @Lob/byte array field with:

   @ContentId
   private String contentId;
    
   @ContentLength
   private long contentLength = 0L;
    
   @MimeType
   private String mimeType;

为您的图像创建头像“商店”:

AvatarStore.java

public interface AvatarStore extends ContentStore<User, String> {
}

这就是创建响应内容类型的REST端点@ /users/{userId}所需要的。当您的应用程序启动时,Spring Content将查看您的依赖项(请参阅Spring Content JPA / REST),查看您的AvatarStore接口,并为JPA注入该接口的实现。它还将注入@Controller来将http请求转发到该实现。这样可以省去您自己实现任何一个过程。

所以...

curl -X POST /users/{userId} -F 'file=@path/to/local/file.jpg'

将化身path/to/local/file.jpg存储在数据库中,并将其与ID为userId的User实体相关联。

curl /users/{usersId} -H 'Accept: image/jpg'

将再次获取它,依此类推...支持完整的CRUD(并且顺便说一句,视频流也是如此)

有入门指南和视频here。 Spring Content JPA的参考指南为here

HTH