SpringBootインタフェースは画像を返します

859 ワード

  • ResponseEntityを使用して結果を返し、image/png
  • などのHttpHeaderのcontent-typeを設定します.
        @RequestMapping(method = RequestMethod.GET)
        public ResponseEntity getFile(@RequestParam long id) {
            Result result = fileService.getFile(id);
            if (result.getCode() == 1) {
                MediaType mediaType = MediaType.parseMediaType(result.getMsg());
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(mediaType);
                ResponseEntity e = new ResponseEntity(result.getData(), headers, HttpStatus.OK);
                return e;
            }
            return ResponseEntity.status(404).body(result.getMsg());
        }
  • 第2の方法:@RequestMappingにproducesを加えてピクチャタイプを設定し、HttpHeaders
  • を個別に設定する必要はありません.
    @RequestMapping(method = RequestMethod.GET, produces = "image/png")