这里用springMVC做个例子,前端请求以下方法时,直接返回一张图片
@RequestMapping("/showPhoto")
public void showPhoto(String photoID, HttpServletResponse response) {
OutputStream outputStream = null;
try {
byte[] photoByte = photo.getPhoto(photoID);
outputStream = response.getOutputStream();
outputStream.write(photoByte);
outputStream.flush();
} catch (Exception e) {
logger.error("error", e);
} finally {
try {
outputStream.close();
} catch (IOException e) {
logger.error("error", e);
}
}
}