国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Javascript開發(fā)之js壓縮篇
Extjs可以說是目前最優(yōu)秀的js開發(fā)庫了。除了那個蹩腳的GPLV3授權。
但是使用中遇到的第一個問題就是,Extjs那龐大的個頭。想辦法壓縮ExtJS的大小成了首先要解決的問題。
談談我的解決方法,歡迎拍磚。突然發(fā)現前面那個廣告貼被鎖了
 

1、壓縮混淆
   除了ExtJS的大個子以外,引用的很多其他的js庫,項目中自己的js文件等等。采用OPOA組件式開發(fā)最后一定會增大js文件的總量。所以項目后期要對這些文件進行壓縮合并?,F在流行的js壓縮工具有很多,如packer,jsMin,Esc,JSA,yui-compressor等等。經過實際使用我選的是yui-compressor.
   yui-compressor項目地址:http://developer.yahoo.com/yui/compressor/
下載后得到一個java開發(fā)的jar包。使用方法基于命令行:
   java -jar yuicompressor-x.y.z.jar [options] [input file]
開發(fā)中的js文件不可能一個個的手動壓縮,這里用到了ANT。在項目構建中可以替你完成以上任務。
Java代碼
  1. <property name="yuicompressor" value="${tools}/yuicompressor-2.3.6.jar" />   
  2.       <apply executable="java" parallel="false" verbose="true" dest="${dist}/WebRoot">   
  3.           <fileset dir="${dist}/WebRoot">   
  4.               <include name="modules/*.js" />   
  5.               <include name="js/*.js" />   
  6.           </fileset>   
  7.           <arg line="-jar" />   
  8.           <arg path="${yuicompressor}" />   
  9.           <arg line="--type js --charset UTF-8 -o" />   
  10.           <targetfile />   
  11.           <mapper type="glob" from="*.js" to="*-m.js" />   
  12.       </apply>   
  13.       <concat destfile="${dist}/WebRoot/js/base.js" encoding="UTF-8" >   
  14.           <fileset dir="${dist}/WebRoot/js">   
  15.               <include name="*-m.js" />   
  16.           </fileset>   
  17.       </concat>   
  18.       <concat destfile="${dist}/WebRoot/modules/modules.js" encoding="UTF-8" >   
  19.           <fileset dir="${dist}/WebRoot/modules">   
  20.               <include name="*-m.js" />   
  21.           </fileset>   
  22.       </concat>   
  23.       <delete>   
  24.           <fileset dir="${dist}/WebRoot">   
  25.               <include name="js/*.js"/>   
  26.               <include name="modules/*.js"/>   
  27.               <exclude name="modules/modules.js" />   
  28.               <exclude name="js/base.js" />   
  29.           </fileset>   
  30.       </delete>  



2、gzip壓縮。
   壇子里討論的gzip壓縮有2種,
   一是有的容器(服務器)提供的功能,但這個局限于特定容器。比如apache+tomcat或者resin-pro版。
   二是部署前手動gzip壓縮,配合servlet過濾器使用,這個能實現gzip功能,但是降低了靈活性。
   我自己用的是自己的實現,采用gzip servlet filter實現。下面是我的代碼參考網上內容.
Java代碼
  1.   
  2. package sh.blog.util.web.filter;   
  3.   
  4. import java.io.IOException;   
  5.   
  6. import java.util.zip.GZIPOutputStream;   
  7.   
  8. import javax.servlet.ServletOutputStream;   
  9.   
  10. public class CompressedStream extends ServletOutputStream {   
  11.        
  12.     private ServletOutputStream out;   
  13.     private GZIPOutputStream    gzip;   
  14.   
  15.     /**  
  16.      * 指定壓縮緩沖流  
  17.      * @param 輸出流到壓縮  
  18.      * @throws IOException if an error occurs with the {@link GZIPOutputStream}.  
  19.      */  
  20.     public CompressedStream(ServletOutputStream out) throws IOException {   
  21.         this.out = out;   
  22.         reset();   
  23.     }   
  24.   
  25.     /** @see ServletOutputStream * */  
  26.     public void close() throws IOException {   
  27.         gzip.close();   
  28.     }   
  29.   
  30.     /** @see ServletOutputStream * */  
  31.     public void flush() throws IOException {   
  32.         gzip.flush();   
  33.     }   
  34.   
  35.     /** @see ServletOutputStream * */  
  36.     public void write(byte[] b) throws IOException {   
  37.         write(b, 0, b.length);   
  38.     }   
  39.   
  40.     /** @see ServletOutputStream * */  
  41.     public void write(byte[] b, int off, int len) throws IOException {   
  42.         gzip.write(b, off, len);   
  43.     }   
  44.   
  45.     /** @see ServletOutputStream * */  
  46.     public void write(int b) throws IOException {   
  47.         gzip.write(b);   
  48.     }   
  49.   
  50.     /**  
  51.      * Resets the stream.  
  52.      *   
  53.      * @throws IOException if an I/O error occurs.  
  54.      */  
  55.     public void reset() throws IOException {   
  56.         gzip = new GZIPOutputStream(out);   
  57.     }   
  58. }  


Java代碼
  1. package sh.blog.util.web.filter;   
  2.   
  3. import java.io.IOException;   
  4. import java.io.PrintWriter;   
  5.   
  6. import javax.servlet.ServletOutputStream;   
  7.   
  8. import javax.servlet.http.HttpServletResponse;   
  9. import javax.servlet.http.HttpServletResponseWrapper;   
  10.   
  11. public class CompressionResponse extends HttpServletResponseWrapper {   
  12.        
  13.     protected HttpServletResponse response;   
  14.     private ServletOutputStream out;   
  15.     private CompressedStream compressedOut;   
  16.     private PrintWriter writer;   
  17.     protected int contentLength;   
  18.   
  19.     /**  
  20.      * 創(chuàng)建一個新的被壓縮響應給HTTP  
  21.      *   
  22.      * @param response the HTTP response to wrap.  
  23.      * @throws IOException if an I/O error occurs.  
  24.      */  
  25.     public CompressionResponse(HttpServletResponse response) throws IOException {   
  26.         super(response);   
  27.         this.response = response;   
  28.         compressedOut = new CompressedStream(response.getOutputStream());   
  29.     }   
  30.   
  31.     /**  
  32.      * Ignore attempts to set the content length since the actual content length  
  33.      * will be determined by the GZIP compression.  
  34.      *   
  35.      * @param len the content length  
  36.      */  
  37.     public void setContentLength(int len) {   
  38.         contentLength = len;   
  39.     }   
  40.   
  41.     /** @see HttpServletResponse * */  
  42.     public ServletOutputStream getOutputStream() throws IOException {   
  43.         if (null == out) {   
  44.             if (null != writer) {   
  45.                 throw new IllegalStateException("getWriter() has already been called on this response.");   
  46.             }   
  47.             out = compressedOut;   
  48.         }   
  49.         return out;   
  50.     }   
  51.   
  52.     /** @see HttpServletResponse * */  
  53.     public PrintWriter getWriter() throws IOException {   
  54.         if (null == writer) {   
  55.             if (null != out) {   
  56.                 throw new IllegalStateException("getOutputStream() has already been called on this response.");   
  57.             }   
  58.             writer = new PrintWriter(compressedOut);   
  59.         }   
  60.         return writer;   
  61.     }   
  62.   
  63.     /** @see HttpServletResponse * */  
  64.     public void flushBuffer() {   
  65.         try {   
  66.             if (writer != null) {   
  67.                 writer.flush();   
  68.             } else if (out != null) {   
  69.                 out.flush();   
  70.             }   
  71.         } catch (IOException e) {   
  72.             e.printStackTrace();   
  73.         }   
  74.     }   
  75.   
  76.     /** @see HttpServletResponse * */  
  77.     public void reset() {   
  78.         super.reset();   
  79.         try {   
  80.             compressedOut.reset();   
  81.         } catch (IOException e) {   
  82.             throw new RuntimeException(e);   
  83.         }   
  84.     }   
  85.   
  86.     /** @see HttpServletResponse * */  
  87.     public void resetBuffer() {   
  88.         super.resetBuffer();   
  89.         try {   
  90.             compressedOut.reset();   
  91.         } catch (IOException e) {   
  92.             throw new RuntimeException(e);   
  93.         }   
  94.     }   
  95.   
  96.     /**  
  97.      * Finishes writing the compressed data to the output stream. Note: this  
  98.      * closes the underlying output stream.  
  99.      *   
  100.      * @throws IOException if an I/O error occurs.  
  101.      */  
  102.     public void close() throws IOException {   
  103.         compressedOut.close();   
  104.     }   
  105. }  

Java代碼
  1. /**  
  2.  * 如果瀏覽器支持解壓縮,則壓縮該代碼  
  3.  * @throws IOException ServletException if an error occurs with the {@link GZIPOutputStream}.  
  4.  * 如果需要開啟該過濾器,在web.xml中加入此代碼  
  5.     <filter>  
  6.       <filter-name>gzip</filter-name>  
  7.       <filter-class>com.strongit.finance.gzip</filter-class>  
  8.     </filter>  
  9.     <filter-mapping>  
  10.       <filter-name>gzip</filter-name>  
  11.       <url-pattern>*.jsp</url-pattern>  
  12.     </filter-mapping>  
  13.  */  
  14.   
  15. package sh.blog.util.web.filter;   
  16.   
  17. import java.io.IOException;   
  18.   
  19. import java.util.Enumeration;   
  20.   
  21. import javax.servlet.Filter;   
  22. import javax.servlet.FilterChain;   
  23. import javax.servlet.FilterConfig;   
  24. import javax.servlet.ServletException;   
  25. import javax.servlet.ServletRequest;   
  26. import javax.servlet.ServletResponse;   
  27.   
  28. import javax.servlet.http.HttpServletRequest;   
  29. import javax.servlet.http.HttpServletResponse;   
  30.   
  31. import org.apache.commons.logging.Log;   
  32. import org.apache.commons.logging.LogFactory;   
  33.   
  34.   
  35. public class CompressionFilter implements Filter {   
  36.   
  37.     protected Log log = LogFactory.getFactory().getInstance(this.getClass().getName());   
  38.   
  39.     @SuppressWarnings("unchecked")   
  40.     public void doFilter(ServletRequest request, ServletResponse response,   
  41.             FilterChain chain) throws IOException, ServletException {   
  42.            
  43.         boolean compress = false;   
  44.         if (request instanceof HttpServletRequest){   
  45.             HttpServletRequest httpRequest = (HttpServletRequest) request;   
  46.             Enumeration headers = httpRequest.getHeaders("Accept-Encoding");   
  47.             while (headers.hasMoreElements()){   
  48.                 String value = (String) headers.nextElement();   
  49.                 if (value.indexOf("gzip") != -1){   
  50.                     compress = true;   
  51.                 }   
  52.             }   
  53.         }   
  54.            
  55.         if (compress){//如果瀏覽器支持則壓縮   
  56.             HttpServletResponse httpResponse = (HttpServletResponse) response;   
  57.             httpResponse.addHeader("Content-Encoding""gzip");   
  58.             CompressionResponse compressionResponse= new CompressionResponse(httpResponse);   
  59.             chain.doFilter(request, compressionResponse);   
  60.             compressionResponse.close();   
  61.         }   
  62.         else{//如果瀏覽器不支持則不壓縮   
  63.             chain.doFilter(request, response);   
  64.         }   
  65.     }   
  66.        
  67.        
  68.     public void init(FilterConfig config) throws ServletException {   
  69.            
  70.     }   
  71.   
  72.     public void destroy(){   
  73.            
  74.     }   
  75. }  


實際使用的效果以http://demo.slivercrm.cn:8084為例

登陸窗口:





首頁:




  • 大小: 65.9 KB
  • 大小: 19.4 KB
  • 大小: 23.9 KB
  • 大小: 29.9 KB
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Java壓縮技術(五) GZIP相關——瀏覽器解析
jsp生成靜態(tài)頁面
使用commons-fileupload例子--孤獨的日子
使用XML文件來實現對Servlet的配置
Servlet 實現隨機驗證碼
Google App Engine性能調優(yōu) - 頁面性能優(yōu)化 - YongPei - JavaEye技術網站
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服