How to get a binary stream by GridFS ObjectId with Spring Data MongoDB

前端 未结 10 1243
暗喜
暗喜 2021-02-02 15:00

I can\'t figure out how to stream a binary file from GridFS with spring-data-mongodb and its GridFSTemplate when I already have the right ObjectId.

相关标签:
10条回答
  • 2021-02-02 15:23
    GridFSDBFile file = ... 
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    file.writeTo(baos);
    byte[] ba = baos.toByteArray()
    
    0 讨论(0)
  • 2021-02-02 15:24

    Wrap the GridFSFile in a GridFsResource or use this

    GridFSFile file = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(fileId)));
    GridFsResource resource = gridFsTemplate.getResource(file);
    return resource.getInputStream();
    
    0 讨论(0)
  • 2021-02-02 15:31

    Have you considered using Spring Content for Mongo for the content storage piece on your solution?

    Assuming you are using Spring Boot as well as Spring Data Mongo then it might look something like the following:

    pom.xml

    <dependency>
        <groupId>com.github.paulcwarren</groupId>
        <artifactId>spring-content-mongo-boot-starter</artifactId>
        <version>0.0.10</version>
    </dependency>
    <dependency>
        <groupId>com.github.paulcwarren</groupId>
        <artifactId>spring-content-rest-boot-starter</artifactId>
        <version>0.0.10</version>
    </dependency>
    

    Update your Spring Data Mongo entity, with the following attributes:

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

    Add a store interface:

    @StoreRestResource(path="content")
    public interface MongoContentStore extends ContentStore<YourEntity, String> {
    }
    

    That's all that you need. When you application starts Spring Content will see the dependencies on the Spring Content Mongo/REST modules and it will inject an implementation of the MongonContenStore store for GridFs as well as an implementation of a controller that supports full CRUD functionality and maps those operations down onto the underlying store interface. The REST endpoint will be available under /content.

    i.e.

    curl -X PUT /content/{entityId} will create or update an entity's image

    curl -X GET /content/{entityId} will fetch the entity's image

    curl -X DELETE /content/{entityId} will delete the entity's image

    There are a couple of getting started guides here. They use Spring Content for the filesystem but the modules are interchangeable. The Mongo reference guide is here. And there is a tutorial video here.

    HTH

    0 讨论(0)
  • 2021-02-02 15:31
    @RequestMapping(value = "/api ")
    public class AttachmentController {
    
    private final GridFsOperations gridFsOperations;
    
    @Autowired
    public AttachmentController(GridFsOperations gridFsOperations) {
        this.gridFsOperations = gridFsOperations;
    }
    
    @GetMapping("/file/{fileId}")
    public ResponseEntity<Resource> getFile(@PathVariable String fileId) {
    GridFSFile file = 
    gridFsOperations.findOne(Query.query(Criteria.where("_id").is(fileId)));
    
        return ResponseEntity.ok()
                .contentLength(file.getLength())
                .body(gridFsOperations.getResource(file));
    }
    
    0 讨论(0)
提交回复
热议问题