关于spring:将数据内容直接从数据库流传输到HTTP

Stream the data content directly from database to the HTTP

现在,我们将文件保存在我们的postgresql数据库中,并使用我们实体中的byte[]字段映射该内容。我需要调查我们是否可以
将内容数据直接从数据库流传输到HTTP输出流,并以相反的方式执行相同的操作,因此使用jpa Blob数据类型将二进制数据从HTTP流传输到数据库。我知道Blob具有方法getBinaryStreamsetBinaryStream,因此它可以工作,并且我们不需要将数据保存到内存中。

我担心的是数据库事务,因为我们正在将实体映射到DTO中,第二件事是Http请求中断,并且数据可能会在某些时候丢失。

有人对这种解决方案有经验吗?


从BLOB读取数据流的解决方案:

现有的BLOB数据通过将OutputStream(由servlet容器提供)传递到事务处理方法中进行流处理,该方法将实体blob数据从内部事务写入流中。请注意,响应的内容类型是在写入数据之前设置的。

实体类:

1
2
3
4
public class Attachment {
   private java.sql.Blob DATA;
   public java.sql.Blob getData() { RETURN DATA; }
}

服务方式:

1
2
3
4
5
6
7
8
9
10
11
@Transactional(readOnly = TRUE)  
public void copyContentsTo(long attachmentId, OutputStream outputStream) throws IOException {
  Attachment dbAttachment = attachmentRepository.findOne(attachmentId);

  try (InputStream IS = dbAttachment.getData().getBinaryStream()) {
    IOUtils.copy(IS, outputStream);

  } catch (SQLException e) {
    throw NEW ParameterException("Cannot extract BLOB for attachment #" + attachmentId, e);
  }
}

REST API Spring Controller方法:

1
2
3
4
5
6
7
8
9
10
@GetMapping(VALUE ="/api/project-attachment/{attachment-id}/content")
@ResponseStatus(HttpStatus.OK)
public void getAttachmentContent(
    @PathVariable("attachment-id") long attachmentId,
    HttpServletResponse response,
    OutputStream stream) throws IOException {

    response.setContentType(getMime(attachmentId));
    attachmentService.copyContentsTo(attachmentId, stream);
}

Lucasz,JPA的Spring Content完全可以满足您的要求。旨在使创建处理内容(文档,图像,视频等)的Spring应用程序变得非常容易。它支持一系列后端存储,其中一个是关系数据库,显然它们使用BLOB。

此JPA模块会将上传的文件从请求输入流直接流式传输到数据库,反之亦然,因此它永远不会将整个文件存储在内存中,这显然会导致非常大的文件出现问题。

这将使您不必在@tequilacat的答案中编写任何代码。

可能值得一看。