- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.io.Reader;
- import java.io.Writer;
- import java.util.List;
- import java.util.StringTokenizer;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.IOUtils;
- import org.apache.commons.lang.StringUtils;
- import org.mozilla.javascript.ErrorReporter;
- import org.mozilla.javascript.EvaluatorException;
- import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
- public class JSControl extends HttpServlet {
- String alljs;
- String jsPath = "js";
- public JSControl() {
- super();
- }
- public void destroy() {
- super.destroy(); // Just puts "destroy" string in log
- // Put your code here
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String allPath = getwebrootPath() + jsPath + File.separator; //获取JS存放目录的绝对路径
- String minFilePath = allPath + alljs + ".min.js"; //生成的压缩文件名字
- File minFile = new File(minFilePath);
- if (!minFile.exists()) {//判断是否已经生成了,如没有这个文件既生成
- Long startTime = System.currentTimeMillis();
- String tempFilePath = allPath + alljs + ".js"; //先合并再压缩,所以这个是合并时的临时文件名
- StringBuffer mergeNr = new StringBuffer();
- alljs = request.getParameter("alljs");
- StringTokenizer st = new StringTokenizer(alljs, ",");
- File tempFile = new File(tempFilePath);
- while (st.hasMoreTokens()) {
- String jsName = st.nextToken();
- File tempJsFile = new File(allPath + jsName + ".js");
- if (tempJsFile.exists()) {
- List<String> stList = FileUtils.readLines(tempJsFile,
- "UTF-8");
- for (String str : stList) {
- mergeNr.append(str).append("\\n"); //这里之前我没加\\n丫的我知道错了,居然都被注释了.哈哈!
- }
- }
- }
- FileUtils.writeStringToFile(tempFile, mergeNr.toString(), "UTF-8",
- false);
- Reader reader = new InputStreamReader(new FileInputStream(tempFile),
- "UTF-8");
- Writer writer = new OutputStreamWriter(new FileOutputStream(minFilePath),
- "UTF-8");
- //这个是YUI Compressor的压缩方法.
- JavaScriptCompressor compressor = new JavaScriptCompressor(reader,
- new ErrorReporter() {
- public void warning(String message, String sourceName,
- int line, String lineSource, int lineOffset) {
- if (line < 0) {
- System.err.println("\\n[WARNING] " + message);
- } else {
- System.err.println("\\n[WARNING] " + line + ':'
- + lineOffset + ':' + message);
- }
- }
- public void error(String message, String sourceName,
- int line, String lineSource, int lineOffset) {
- if (line < 0) {
- System.err.println("\\n[ERROR] " + message);
- } else {
- System.err.println("\\n[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(writer, -1, true, false, false, false);
- reader.close();
- writer.close();
- tempFile.delete();
- System.out.println("压缩时间>>>>>>>>>"
- + (System.currentTimeMillis() - startTime));
- }
- // response.sendRedirect(request.getContextPath() + "/js/" + alljs
- // + ".min.js");
- getServletContext().getRequestDispatcher(
- request.getContextPath() + "/js/" + alljs + ".min.js").forward(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doGet(request, response);
- }
- public void init() throws ServletException {
- }
- private final static String getWebrootPath() {
- String root = JSControl.class.getResource("/").getFile();
- try {
- root = new File(root).getParentFile().getParentFile()
- .getCanonicalPath();
- root += File.separator;
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- return root;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/270620134317.html
来源: http://www.codesnippet.cn/detail/270620134317.html