派生自 projectDept/qhighschool

EricsHu
2023-04-10 5196a1d1e00ced009a2d4e8ea0aa5f3bb7cefe33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package com.qxueyou.scc.base.util;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
 
import org.apache.commons.lang3.StringUtils;
 
/**
 * 前三个属性根据实际存储位置设置
 * @author ody.yuan
 *
 */
public class MyJsCssVersionUtils {
    
     /** web工程的发布路径  */
    private static String webContent = "qxueyou";
    
    /** web工程中web文件夹的绝对路径 D:\project\workspace_web\versioncontroller0\web\js\qxueyou.js  */
    /** web工程中web文件夹的绝对路径 D:\project\workspaceStkkk\qxueyou_web\web\js\qxueyou.js  */
    private static String webFileAbsolutePath = "C:" + File.separator + "workspace" + File.separator + "qxueyou_web";
    
    /** 扫描js、css、html文件的文件夹绝对路径,固定设置为web目录  */
    private static String fileScanDirec = "C:" + File.separator + "workspace" + File.separator + "qxueyou_web";
    
    /** 匹配html中引入js文件  */
    private static Pattern scriptImportPattrrn = Pattern.compile("<script(.*)src(.*)</script>");
    
    /** 匹配html中引入css文件  */
    private static Pattern cssImportPattrrn = Pattern.compile("<link(.*)href(.*)rel(.*)>");
    
    private static char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    
    
    /**
     * 得到处理过的文件路径为key,文件md5值为value的map
     * @param filePath 文件路径
     * @param fileExtension 文件类型
     * @param map map
     */
    private static void getFileMD5Map(String filePath,String fileExtension,Map<String,String> map,String version){
        readFile(filePath,fileExtension,map,version);
    }
    
    /**
     * 得到路径下的所需文件和文件的md5值
     * @param filePath
     * @param fileExtension
     * @param map
     */
    private static void readFile(String filePath,String fileExtension,Map<String,String> map,String version){
        
        File file = new File(filePath);
        if(file.isFile()){
            String fiePath = file.getAbsolutePath();
            //符合指定类型
            if(fiePath.endsWith(fileExtension)){
                generateMap(file,fileExtension,map,version);
            }
        }else if(file.isDirectory()){
            String[] fileList = file.list();
            for(String name : fileList){
                readFile(filePath + File.separator + name,fileExtension,map,version);
            }
        }
    }
    
    /**
     * 得到一个文件的map
     * @param file
     * @param map
     */
    private static void generateMap(File file, String fileExtension, Map<String,String> map,String version){
        //TODO  缩短md5值
        String strKey = file.getAbsolutePath();
        strKey = strKey.replace(webFileAbsolutePath, File.separator + webContent);
        strKey = strKey.replace("\\", "/");
        String strValue = getMD5(file);
        if(StringUtils.isNotBlank(strValue)){
            strValue = version;
        }
        map.put(strKey, strValue);
        
    }
    
    /**
     * 对文件全文生成MD5摘要
     * 
     * @param file
     *            要加密的文件
     * @return MD5摘要码
     */
    public static String getMD5(File file) {
        FileInputStream fis = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            fis = new FileInputStream(file);
            byte[] buffer = new byte[2048];
            int length = -1;
            //暂时不用
            //long s = System.currentTimeMillis();
            while ((length = fis.read(buffer)) != -1) {
                md.update(buffer, 0, length);
            }
            byte[] b = md.digest();
            return byteToHexString(b);
            // 16位加密
            // return buf.toString().substring(8, 24);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
 
    /**
     * 把byte[]数组转换成十六进制字符串表示形式
     * @param tmp    要转换的byte[]
     * @return 十六进制字符串表示形式
     */
    private static String byteToHexString(byte[] tmp) {
        
        String s;
        // 用字节表示就是 16 个字节
        char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话,使用两个字符,
        // 所以表示成 16 进制需要 32 个字符
        int k = 0; // 表示转换结果中对应的字符位置
        for (int i = 0; i < 16; i++) { // 从第一个字节开始,对 MD5 的每一个字节
            // 转换成 16 进制字符的转换
            byte byte0 = tmp[i]; // 取第 i 个字节
            str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换, 
            // >>> 为逻辑右移,将符号位一起右移
            str[k++] = hexDigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换
        }
        s = new String(str); // 换后的结果转换为字符串
        return s;
    }
    
    /**
     * 
     * @param filePath
     * @param fileExtension
     * @param map
     */
    private static void getHtml(String filePath,String fileExtension,Map<String,String> map){
        readHtmlFile(fileScanDirec,".ftl",map);
    }
    
    /**
     * 得到路径下的所需文件和文件的md5值
     * @param filePath
     * @param fileExtension
     * @param map
     */
    private static void readHtmlFile(String filePath,String fileExtension,Map<String,String> map){
        
        File file = new File(filePath);
        if(file.isFile()){
            String fiePath = file.getAbsolutePath();
            //符合指定类型
            if(fiePath.endsWith(fileExtension)){
                addVersion(file,map);
            }
        }else if(file.isDirectory()){
            String[] fileList = file.list();
            for(String name : fileList){
                readHtmlFile(filePath + File.separator + name,fileExtension,map);
            }
        }
    }
    
    /**
     * 得到一个文件的map
     * @param file
     * @param map
     */
    private static void addVersion(File file,Map<String,String> map){
        //TODO  缩短md5值
        InputStream is = null;
        OutputStream os = null;
        List<String> contentList = new ArrayList<String>();
        
        // 读文件
        try {
            //指定utf-8格式
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
            String line = null;
            
            while ((line = br.readLine()) != null) {
                //TODO version 传null值
                String modLine = getModLine(line, null,map);
                if (modLine != null) {
                    //System.out.println(modLine);
                    line = modLine;
                }
                line = line + "\r\n";
                contentList.add(line);
            }
            // 关闭流
            br.close();
        } catch (Exception e) {
            System.out.println("读文件失败:" + file.getAbsolutePath());
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
 
        }
        // 写文件
        try {
            //指定utf8格式
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
            
            for (Iterator<String> it = contentList.iterator(); it.hasNext();) {
                String line = it.next();
                bw.write(line);
            } 
            // 更新到文件
            bw.flush();
            // 关闭流
            bw.close();
        } catch (Exception e) {
            System.out.println("写文件失败:" + file.getAbsolutePath());
            e.printStackTrace();
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 
     * 查找文件需要的版本号的js 与css 行
     * @param line 行字符
     * @param version 默认版本
     * @param map md5 map
     * @return 
     */
    private static String getModLine(String line, String version, Map<String, String> map) {
        
        //匹配html引入js文件       eg:<script type="text/javascript" src="/qxueyou/web/js/qxueyou.js"></script>
        if (scriptImportPattrrn.matcher(line).find()) {
            //以引号分隔,以/开始
            String md5Version = null;
            String strLine[] = line.split("\"");
            for(String str:strLine){
                if(null != md5Version){
                    break;
                }
                if(str.startsWith("/")){
                    //如果已有版本号,按?分隔,取第一个
                    if(str.lastIndexOf("?version") != -1){
                        str = str.substring(0, str.lastIndexOf("?version"));
                    }
                    md5Version = map.get(str);
                }
            }
            
            int pos = line.lastIndexOf(".js");
            if(null != md5Version){
                String modLine = line.substring(0, pos) + ".js?version=" + md5Version+ "\"></script>";
                return modLine;
            }
        
        //匹配html引入css文件      eg:<link href="/qxueyou/web/css/bootstrap.min.css" rel="stylesheet">
        } else if (cssImportPattrrn.matcher(line).find()) {
            
            //以引号分隔,以/开始
            String md5Version = null;
            String strLine[] = line.split("\"");
            for(String str:strLine){
                if(null != md5Version){
                    break;
                }
                if(str.startsWith("/")){
                    //如果已有版本号,按?分隔,取第一个
                    if(str.lastIndexOf("?version") != -1){
                        str = str.substring(0, str.lastIndexOf("?version"));
                    }
                    md5Version = map.get(str);
                }
            }
            
            int pos = line.lastIndexOf(".css");
            if(null != md5Version){
                String modLine = line.substring(0, pos) + ".css?version=" + md5Version+ "\" rel=\"stylesheet\">";
                return modLine;
            }
            
        }
        
        return line;
    }
 
    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
        String version = df.format(date);
        
        Map<String,String> objMap = new HashMap<String,String>();
        //添加js文件
        getFileMD5Map(fileScanDirec,".js",objMap,version);
        //添加CSS文件
        getFileMD5Map(fileScanDirec,".css",objMap,version);
        
        //循环html文件,修改其中的css和js引入路径
        getHtml(fileScanDirec,".ftl",objMap);
        
        System.out.println("successful");
        
    }
 
}