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(""); /** Æ¥ÅähtmlÖÐÒýÈëcssÎļþ */ private static Pattern cssImportPattrrn = Pattern.compile(""); 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 map,String version){ readFile(filePath,fileExtension,map,version); } /** * µÃµ½Â·¾¶ÏµÄËùÐèÎļþºÍÎļþµÄmd5Öµ * @param filePath * @param fileExtension * @param map */ private static void readFile(String filePath,String fileExtension,Map 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 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 map){ readHtmlFile(fileScanDirec,".ftl",map); } /** * µÃµ½Â·¾¶ÏµÄËùÐèÎļþºÍÎļþµÄmd5Öµ * @param filePath * @param fileExtension * @param map */ private static void readHtmlFile(String filePath,String fileExtension,Map 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 map){ //TODO Ëõ¶Ìmd5Öµ InputStream is = null; OutputStream os = null; List contentList = new ArrayList(); // ¶ÁÎļþ 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 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 map) { //Æ¥ÅähtmlÒýÈëjsÎļþ eg: 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+ "\">"; return modLine; } //Æ¥ÅähtmlÒýÈëcssÎļþ eg£º } 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 objMap = new HashMap(); //Ìí¼ÓjsÎļþ getFileMD5Map(fileScanDirec,".js",objMap,version); //Ìí¼ÓCSSÎļþ getFileMD5Map(fileScanDirec,".css",objMap,version); //Ñ­»·htmlÎļþ£¬ÐÞ¸ÄÆäÖеÄcssºÍjsÒýÈë·¾¶ getHtml(fileScanDirec,".ftl",objMap); System.out.println("successful"); } }