码迷,mamicode.com
首页 > 其他好文 > 详细

Easyui 页面訪问慢解决方式,GZIP站点压缩加速优化

时间:2017-05-16 21:37:34      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:--   div   private   溢出   eth   相等   cas   wrap   except   



1. 静态资源压缩
GZIP是站点压缩加速的一种技术,对于开启后能够加快我们站点的打开速度。原理是经过server压缩,client浏览器高速解压的原理,能够大大降低了站点的流量。


详细代码能够參加jeecg高速开发平台的实现;

通过资源压缩拦截器,降低带宽訪问

參考代码:

/**
 * JS缓存压缩
 * JEECG开源社区
 * 论坛:www.jeecg.org
 * @author  张代浩
 */
public class GZipFilter implements Filter {

	
    public void destroy() {
    }
      /**
       * 推断浏览器是否支持GZIP
       * @param request
       * @return
       */
      private static boolean isGZipEncoding(HttpServletRequest request){
        boolean flag=false;
        String encoding=request.getHeader("Accept-Encoding");
          //update-begin--Author:JueYue  Date:20140518 for:IE下Excel上传encode为空的bug--------------------
        if(encoding!=null&&encoding.indexOf("gzip")!=-1){
          flag=true;
        }
          //update-end--Author:JueYue  Date:20140518 for:IE下Excel上传encode为空的bug--------------------
         return flag;
      }
      
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpServletRequest req=(HttpServletRequest)request;
        if(isGZipEncoding(req)){
            Wrapper wrapper = new Wrapper(resp);
            chain.doFilter(request, wrapper);
            byte[] gzipData = gzip(wrapper.getResponseData());
            resp.addHeader("Content-Encoding", "gzip");
            resp.setContentLength(gzipData.length);
            //静态资源文件缓存机制
            //CacheResource(request, response, chain);
            ServletOutputStream output = response.getOutputStream();
            output.write(gzipData);
            output.flush();
        } else {
            chain.doFilter(request, response);
        }        

    }

	public void init(FilterConfig filterConfig) throws ServletException {}
    
    /**
     * 提高系统訪问性能,主键缓存
     */
    public void CacheResource(ServletRequest request, ServletResponse response,
            FilterChain chain){
    	//1.强转httpservlet,方便调用方法   
        HttpServletRequest req = (HttpServletRequest) request;  
        HttpServletResponse res = (HttpServletResponse) response;  
        //2.获取资源文件名称的URI   
        String uri = req.getRequestURI();  
        //3.获得文件扩展名,lastIndexOf(".")+1 获得.最后一次出现的索引的后一位:jpg   
        uri = uri.substring(uri.lastIndexOf(".")+1);  
        System.out.println( uri );//測试获取后缀是否正确   
        //4断对应后缀文件,设定缓存时间   
        long date = 0;  
        //System.out.println( new Date().getTime());//測试当前时间用   
          
        //推断URI获取的后缀名是否与JPG相等,不考虑大写和小写   
        if(uri.equalsIgnoreCase("jpg")){  
            //读取XML里的JPG配置的參数,这里设定了时间   
            //获取当前系统时间 + 须要缓存的时间(小时),Long 防止溢出。由于单位是毫秒   
            date = System.currentTimeMillis()+5*60*60*1000;  
        }  
          
        if(uri.equalsIgnoreCase("gif")){  
            //读取XML里的JPG配置的參数,这里设定了时间   
            //获取当前系统时间 + 须要缓存的时间(小时),Long 防止溢出,由于单位是毫秒   
            date = System.currentTimeMillis()+5*60*60*1000;  
        }  
          
        if(uri.equalsIgnoreCase("css")){  
            //读取XML里的JPG配置的參数。这里设定了时间   
            //获取当前系统时间 + 须要缓存的时间(小时),Long 防止溢出。由于单位是毫秒   
            date = System.currentTimeMillis()+5*60*60*1000;  
        }  
          
        if(uri.equalsIgnoreCase("js")){  
            //读取XML里的JPG配置的參数,这里设定了时间   
            //获取当前系统时间 + 须要缓存的时间(小时),Long 防止溢出。由于单位是毫秒   
            date = System.currentTimeMillis()+5*60*60*1000;  
        }  
        //设置缓存时间   
        res.setDateHeader("Expires", date);  
    }

    private byte[] gzip(byte[] data) {
        ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(10240);
        GZIPOutputStream output = null;
        try {
            output = new GZIPOutputStream(byteOutput);
            output.write(data);
        } catch (IOException e) {
        } finally {
            try {
                output.close();
            } catch (IOException e) {
            }
        }
        return byteOutput.toByteArray();
    }

}
2. 静态资源缓存
3. easyui 页面优化
   http://www.easyui.info/archives/1435.html
   http://www.360doc.com/content/14/0209/08/9200790_350899585.shtml

Easyui 页面訪问慢解决方式,GZIP站点压缩加速优化

标签:--   div   private   溢出   eth   相等   cas   wrap   except   

原文地址:http://www.cnblogs.com/jzssuanfa/p/6863531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!