需要用到IO流,设置ContentType,代码示例如下:

@RestController@RequestMapping("test")public class Test{
      @Autowired
       private HttpServletResponse response;
       
 @GetMapping("/getHeadImage")
    public void getHeadImage() throws IOException {
        OutputStream os = null;
        BufferedImage image = null;
        try {
            image = ImageIO.read(new FileInputStream(new File("真实的图片绝对路径")));
            response.setContentType("image/png");
            os = response.getOutputStream();
            if (image != null) {
                ImageIO.write(image, "png", os);
            }
        } finally {
            if (os != null) {
                os.flush();
                os.close();
            }
        }
    }}

链接:https://www.jianshu.com/p/0a5de59bda1a