浏览器有默认的缓存机制,不同的浏览器,缓存头是不一样的
设置编码,调用setContentType()方法,参数:”text/html;charset=utf-8”
”Expires”,-1
“Cache-Control”,”no-cache”
“Pragma”,”no-cache”
调用setDateHeaader()方法,参数:”Expires”,System.currentTimeMils()+毫秒,那个毫秒值有时候会变成负数,要用1000l long类型去乘
response.setDateHeader("Last-Modified",new Date().getTime()); response.setDateHeader("Expires",System.currentTimeMillis()+1000l*3600*24*30);//缓存1个月,注意1000后面是l response.setHeader("Cache-Control","Public"); response.setHeader("Pragma","Pragma"); FileInputStream file=new FileInputStream(this.getServletContext().getRealPath("1.jpg")); OutputStream os=response.getOutputStream(); byte[] b=new byte[1024]; int len=0; while((len=file.read(b))!=-1){ os.write(b,0,len); } file.close();
原文链接:/javase/996831.html