How to return an image in Spring Boot controller and serve like a file system

前端 未结 4 1394
无人及你
无人及你 2020-12-08 16:28

I\'ve tried the various ways given in Stackoverflow, maybe I missed something.

I have an Android client (whose code I can\'t change) which is currently getting an im

4条回答
  •  臣服心动
    2020-12-08 17:17

    I believe this should work:

    @RequestMapping(value = "/Image/{id:.+}", method = RequestMethod.GET)
    public ResponseEntity getImage(@PathVariable("id") String id) {
        byte[] image = imageService.getImage(id);
        return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(image);
    }
    

    Notice that the content-type is set for ResponseEntity, not for HttpServletResponse directly.

提交回复
热议问题