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();
}