html的img标签是很常见的,一般的应用是
xml 代码
<img src="/images/abc.jpg"/>
等形式。
其实浏览器在解析html时,遇到img标签后会单独请求src里指定的url。
这样就可以简单的使用img标签来动态加载图片了。
测试程序基于struts,先来写个action
java 代码
- public ActionForward image(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- File file= new File(request.getSession().getServletContext().getRealPath("/images/abc.jpg"));
- if(file.exists()) {
- response.setContentType("image/jpeg");
- ServletOutputStream sos = response.getOutputStream();
- IOUtils.copy(new FileInputStream(file), sos);
- sos.flush();
- sos.close();
- }
- return null;
- }
最后就是写个html文件,里面只写一句就可以了
xml 代码
- <img src="image.do?method=image"/>