@RequestMapping("showImg")
public void genImage(HttpServletResponse resp) throws Exception {
try {
InputStream is = new FileInputStream(new File("e:/PieChart2.png"));
OutputStream os = new BufferedOutputStream(resp.getOutputStream());
byte[] buffer = new byte[1024];
int len = is.read(buffer);
while(len != -1) {
os.write(buffer, 0, len);
len=is.read(buffer);
os.flush();
}
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
然后在页面上访问该controller的路径显示图片
<img src="/showImg" >