本篇文章為大家展示了在SpringBoot中使用@ResponseBody實(shí)現(xiàn)返回圖片功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
首先寫一個(gè)Controller類,包括一個(gè)方法,如下:
package com.example.demo.common; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.io.File; import java.io.FileInputStream; @RestController @RequestMapping(value="/api/v1") public class ImageTest { @GetMapping(value = "/image",produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public byte[] test() throws Exception { File file = new File("E:\\ce\\1.jpg"); FileInputStream inputStream = new FileInputStream(file); byte[] bytes = new byte[inputStream.available()]; inputStream.read(bytes, 0, inputStream.available()); return bytes; } }