浏覽器有默認的緩存機制,不同的浏覽器,緩存頭是不一樣的
設置編碼,調用setContentType()方法,參數:”text/html;charset=utf-8”
關閉緩存,調用setHeader()方法:參數:
”Expires”,-1
“Cache-Control”,”no-cache”
“Pragma”,”no-cache”
開啟緩存,調用setHeader()方法:參數:
調用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();