3 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
如果您使用的是Spring 3.1或更高版本,則可以在@RequestMapping批注中指定“產(chǎn)生” 。下面的示例對(duì)我來(lái)說(shuō)開(kāi)箱即用。如果啟用了Web MVC(@EnableWebMvc),則不需要寄存器轉(zhuǎn)換器或其他任何東西。
@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貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
除了注冊(cè)a之外ByteArrayHttpMessageConverter,您可能還想使用a ResponseEntity代替@ResponseBody。以下代碼對(duì)我有用:
@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 關(guān)注
- 1174 瀏覽
添加回答
舉報(bào)