<img>标签,利用io流从服务器向浏览器写入图片
前提:
D:/DevelopmentTool/apache-tomcat-8.5.14/webapps/pic/服务器该文件夹下存放了名字为001.jpg
的图片
HTML页面:
<img style="width: 550px"src="project/manager/picture?name=001.png&time=${currentTime}">
说明:project/manager是服务器controller的地址@Controller @RequestMapping("project/manager/")
picture是controller里面往外写图片的方法的地址@RequestMapping("picture")time=${currentTime}是为了对付浏览器缓存,${currentTime}是从服务器上传过来的当前系统时间的值服务器controller写法:@Controller @RequestMapping("project/manager/") public class ProjectManagerController extends BaseController { @RequestMapping("picture") public void upload( HttpServletResponse response,String name) { OutputStream os = null; FileInputStream fis = null; try { os = response.getOutputStream(); fis = new FileInputStream("D:/DevelopmentTool/apache-tomcat-8.5.14/webapps/pic/"+name); byte[] buffer = new byte[1024]; while(fis.read(buffer) != -1) { os.write(buffer); } }catch (Exception e){ } finally { if(ValidateUtil.objectIsNotNull(os)){ try { os.flush(); os.close(); fis.close(); }catch (Exception e){ } } } }}