SpringMVC方式:
@RequestMapping(value = "/image/{id}", method = RequestMethod.GET)
public ResponseEntity<byte[]> image(@PathVariable String id) throws IOException {
Map<String, Object> book = bookService.find(id);
if (book == null) {
return new ResponseEntity<byte[]>(HttpStatus.NO_CONTENT);
}
byte[] zp = (byte[]) book.get("bookimage");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<byte[]>(zp, headers, HttpStatus.OK);
}
HttpServlet方式:
@RequestMapping(value = "/getBookImage/{id}", method = RequestMethod.GET)
public void getBookImage(@PathVariable String id, HttpServletResponse response) throws IOException {
Map<String, Object> book = bookService.find(id);
if (book == null) {
return;
}
byte[] zp = (byte[]) book.get("bookimage");
response.setContentType("image/png");
OutputStream stream;
stream = response.getOutputStream();
stream.write(zp);
stream.flush();
stream.close();
}
本文主要探讨了如何使用SpringMVC框架来处理并输出二进制形式的图片,包括两种方法:SpringMVC的方式和HttpServlet的方式。通过实例详细解析了在Web应用中读取和发送二进制图片数据的关键步骤和技术要点。
4278

被折叠的 条评论
为什么被折叠?



