Easyui 頁面訪問慢解決方案,GZIP網站壓縮加速最佳化

來源:互聯網
上載者:User


1. 靜態資源壓縮
GZIP是網站壓縮加速的一種技術,對於開啟後可以加快我們網站的開啟速度,原理是經過伺服器壓縮,用戶端瀏覽器快速解壓的原理,可以大大減少了網站的流量。
具體代碼可以參加 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.