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 關注
- 1165 瀏覽
添加回答
舉報