3 回答

TA贡献1895条经验 获得超7个赞
如果您使用的是Spring 3.1或更高版本,则可以在@RequestMapping批注中指定“产生” 。下面的示例对我来说开箱即用。如果启用了Web MVC(@EnableWebMvc),则不需要寄存器转换器或其他任何东西。
@ResponseBody
@RequestMapping(value = "/photo2", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] testphoto() throws IOException {
InputStream in = servletContext.getResourceAsStream("/images/no_image.jpg");
return IOUtils.toByteArray(in);
}

TA贡献1802条经验 获得超5个赞
除了注册a之外ByteArrayHttpMessageConverter,您可能还想使用a ResponseEntity代替@ResponseBody。以下代码对我有用:
@RequestMapping("/photo2")
public ResponseEntity<byte[]> testphoto() throws IOException {
InputStream in = servletContext.getResourceAsStream("/images/no_image.jpg");
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<byte[]>(IOUtils.toByteArray(in), headers, HttpStatus.CREATED);
}
- 3 回答
- 0 关注
- 1137 浏览
添加回答
举报