JAVA压缩JS或CSS文件

版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/jianggujin/article/details/80202559

平时开发过程中使用的js或css文件基本都是未经压缩的方便调试,在真正上线后一般都会使用压缩后的文件,我们可以使用一些工具进行压缩,我们也可以通过JAVA代码自己实现文件的压缩,今天为大家介绍的是使用YUI Compressor实现的方式。

首先在maven中添加pom坐标

<dependency>
   <groupId>com.yahoo.platform.yui</groupId>
   <artifactId>yuicompressor</artifactId>
   <version>2.4.8</version>
   <exclusions>
      <exclusion>
         <!-- 去除,否则在Web环境下会出错:StringIndexOutOfBoundsException -->
         <groupId>rhino</groupId>
         <artifactId>js</artifactId>
      </exclusion>
   </exclusions>
</dependency>

实现的工具类如下:

package com.jianggujin.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;

import org.apache.log4j.Logger;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;

import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;

/**
 * JS、CSS压缩工具
 * 
 * @author jianggujin
 *
 */
public class CompressorUtils {
   private Logger logger = Logger.getLogger(CompressorUtils.class);

   public void compressJS(File js, Writer out) throws Exception {
      compressJS(js, out, -1, true, true, false, false);
   }

   public void compressJS(File js, Writer out, int linebreakpos, boolean munge, boolean verbose,
         boolean preserveAllSemiColons, boolean disableOptimizations) throws IOException {
      try (InputStreamReader in = new InputStreamReader(new FileInputStream(js), "UTF-8");) {
         JavaScriptCompressor compressor = new JavaScriptCompressor(in, new ErrorReporter() {

            public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) {
               if (logger.isInfoEnabled()) {
                  logger.warn("\n[WARNING] in " + js.getAbsolutePath());
               }
               if (line < 0) {
                  if (logger.isInfoEnabled()) {
                     logger.warn("  " + message);
                  }
               } else {
                  if (logger.isInfoEnabled()) {
                     logger.warn("  " + line + ':' + lineOffset + ':' + message);
                  }
               }
            }

            public void error(String message, String sourceName, int line, String lineSource, int lineOffset) {
               if (logger.isInfoEnabled()) {
                  logger.error("[ERROR] in " + js.getAbsolutePath());
               }
               if (line < 0) {
                  if (logger.isInfoEnabled()) {
                     logger.error("  " + message);
                  }
               } else {
                  if (logger.isInfoEnabled()) {
                     logger.error("  " + line + ':' + lineOffset + ':' + message);
                  }
               }
            }

            public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource,
                  int lineOffset) {
               error(message, sourceName, line, lineSource, lineOffset);
               return new EvaluatorException(message);
            }
         });

         compressor.compress(out, linebreakpos, munge, verbose, preserveAllSemiColons, disableOptimizations);
      }
   }

   public void compressCSS(File css, Writer out) throws Exception {
      compressCSS(css, out, -1);
   }

   public void compressCSS(File css, Writer out, int linebreakpos) throws IOException {
      try (InputStreamReader in = new InputStreamReader(new FileInputStream(css), "UTF-8");) {
         CssCompressor compressor = new CssCompressor(in);

         compressor.compress(out, linebreakpos);
      }
   }
}
阅读更多

扫码向博主提问

蒋固金

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • java
  • oracle
  • js
去开通我的Chat快问

没有更多推荐了,返回首页