How to send an retrieved image from Mongo using GridFS in Spring Rest Call?

前端 未结 1 1547
梦谈多话
梦谈多话 2021-02-10 21:29

I have retrieved the image from Mongo DB using Spring Data and GridFs Template

so i don\'t know how to serve that retrieved input stream back to user .

相关标签:
1条回答
  • 2021-02-10 22:05

    I have used the spring boot and rest where this following code will work if you are using latest version of spring i.e.,Spring 4.1

    @RequestMapping(value = "/image", method = RequestMethod.GET)
        @ResponseBody
        public ResponseEntity<InputStreamResource> getImage() {
            GridFSDBFile gridFsFile = App.getImageResponse();
    
            return ResponseEntity.ok()
                    .contentLength(gridFsFile.getLength())
                    .contentType(MediaType.parseMediaType(gridFsFile.getContentType()))
                    .body(new InputStreamResource(gridFsFile.getInputStream()));
        }
    

    I followed this post , Check out . Spring MVC: How to return image in @ResponseBody?

    0 讨论(0)
提交回复
热议问题