后台如何将图片的byte转成流传给前端展示
目录
后台如何将图片的byte[]转成流传给前端展示
这里用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);
}
}
}