From 068fc7f2e81178e55fa191a13709af64b1a163f6 Mon Sep 17 00:00:00 2001 From: EricsHu <hrr145632> Date: 星期一, 05 十二月 2022 14:27:43 +0800 Subject: [PATCH] 处理编码 --- src/main/java/com/qxueyou/scc/base/util/MyJsCssVersionUtils.java | 118 +++++++++++++++++++++++++++++----------------------------- 1 files changed, 59 insertions(+), 59 deletions(-) diff --git a/src/main/java/com/qxueyou/scc/base/util/MyJsCssVersionUtils.java b/src/main/java/com/qxueyou/scc/base/util/MyJsCssVersionUtils.java index 6a339ba..9806409 100644 --- a/src/main/java/com/qxueyou/scc/base/util/MyJsCssVersionUtils.java +++ b/src/main/java/com/qxueyou/scc/base/util/MyJsCssVersionUtils.java @@ -23,35 +23,35 @@ import org.apache.commons.lang3.StringUtils; /** - * 前三个属性根据实际存储位置设置 + * 鍓嶄笁涓睘鎬ф牴鎹疄闄呭瓨鍌ㄤ綅缃缃� * @author ody.yuan * */ public class MyJsCssVersionUtils { - /** web工程的发布路径 */ + /** 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 */ + /** web宸ョ▼涓瓀eb鏂囦欢澶圭殑缁濆璺緞 D:\project\workspace_web\versioncontroller0\web\js\qxueyou.js */ + /** web宸ョ▼涓瓀eb鏂囦欢澶圭殑缁濆璺緞 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目录 */ + /** 鎵弿js銆乧ss銆乭tml鏂囦欢鐨勬枃浠跺す缁濆璺緞锛屽浐瀹氳缃负web鐩綍 */ private static String fileScanDirec = "C:" + File.separator + "workspace" + File.separator + "qxueyou_web"; - /** 匹配html中引入js文件 */ + /** 鍖归厤html涓紩鍏s鏂囦欢 */ private static Pattern scriptImportPattrrn = Pattern.compile("<script(.*)src(.*)</script>"); - /** 匹配html中引入css文件 */ + /** 鍖归厤html涓紩鍏ss鏂囦欢 */ 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 文件类型 + * 寰楀埌澶勭悊杩囩殑鏂囦欢璺緞涓簁ey锛屾枃浠秏d5鍊间负value鐨刴ap + * @param filePath 鏂囦欢璺緞 + * @param fileExtension 鏂囦欢绫诲瀷 * @param map map */ private static void getFileMD5Map(String filePath,String fileExtension,Map<String,String> map,String version){ @@ -59,7 +59,7 @@ } /** - * 得到路径下的所需文件和文件的md5值 + * 寰楀埌璺緞涓嬬殑鎵�闇�鏂囦欢鍜屾枃浠剁殑md5鍊� * @param filePath * @param fileExtension * @param map @@ -69,7 +69,7 @@ File file = new File(filePath); if(file.isFile()){ String fiePath = file.getAbsolutePath(); - //符合指定类型 + //绗﹀悎鎸囧畾绫诲瀷 if(fiePath.endsWith(fileExtension)){ generateMap(file,fileExtension,map,version); } @@ -82,12 +82,12 @@ } /** - * 得到一个文件的map + * 寰楀埌涓�涓枃浠剁殑map * @param file * @param map */ private static void generateMap(File file, String fileExtension, Map<String,String> map,String version){ - //TODO 缩短md5值 + //TODO 缂╃煭md5鍊� String strKey = file.getAbsolutePath(); strKey = strKey.replace(webFileAbsolutePath, File.separator + webContent); strKey = strKey.replace("\\", "/"); @@ -100,11 +100,11 @@ } /** - * 对文件全文生成MD5摘要 + * 瀵规枃浠跺叏鏂囩敓鎴怣D5鎽樿 * * @param file - * 要加密的文件 - * @return MD5摘要码 + * 瑕佸姞瀵嗙殑鏂囦欢 + * @return MD5鎽樿鐮� */ public static String getMD5(File file) { FileInputStream fis = null; @@ -113,14 +113,14 @@ 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位加密 + // 16浣嶅姞瀵� // return buf.toString().substring(8, 24); } catch (Exception ex) { ex.printStackTrace(); @@ -135,25 +135,25 @@ } /** - * 把byte[]数组转换成十六进制字符串表示形式 - * @param tmp 要转换的byte[] - * @return 十六进制字符串表示形式 + * 鎶奲yte[]鏁扮粍杞崲鎴愬崄鍏繘鍒跺瓧绗︿覆琛ㄧず褰㈠紡 + * @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 位的数字转换 + // 鐢ㄥ瓧鑺傝〃绀哄氨鏄� 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); // 换后的结果转换为字符串 + s = new String(str); // 鎹㈠悗鐨勭粨鏋滆浆鎹负瀛楃涓� return s; } @@ -168,7 +168,7 @@ } /** - * 得到路径下的所需文件和文件的md5值 + * 寰楀埌璺緞涓嬬殑鎵�闇�鏂囦欢鍜屾枃浠剁殑md5鍊� * @param filePath * @param fileExtension * @param map @@ -178,7 +178,7 @@ File file = new File(filePath); if(file.isFile()){ String fiePath = file.getAbsolutePath(); - //符合指定类型 + //绗﹀悎鎸囧畾绫诲瀷 if(fiePath.endsWith(fileExtension)){ addVersion(file,map); } @@ -191,24 +191,24 @@ } /** - * 得到一个文件的map + * 寰楀埌涓�涓枃浠剁殑map * @param file * @param map */ private static void addVersion(File file,Map<String,String> map){ - //TODO 缩短md5值 + //TODO 缂╃煭md5鍊� InputStream is = null; OutputStream os = null; List<String> contentList = new ArrayList<String>(); - // 读文件 + // 璇绘枃浠� try { - //指定utf-8格式 + //鎸囧畾utf-8鏍煎紡 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8")); String line = null; while ((line = br.readLine()) != null) { - //TODO version 传null值 + //TODO version 浼爊ull鍊� String modLine = getModLine(line, null,map); if (modLine != null) { //System.out.println(modLine); @@ -217,10 +217,10 @@ line = line + "\r\n"; contentList.add(line); } - // 关闭流 + // 鍏抽棴娴� br.close(); } catch (Exception e) { - System.out.println("读文件失败:" + file.getAbsolutePath()); + System.out.println("璇绘枃浠跺け璐ワ細" + file.getAbsolutePath()); e.printStackTrace(); } finally { if (null != is) { @@ -232,21 +232,21 @@ } } - // 写文件 + // 鍐欐枃浠� try { - //指定utf8格式 + //鎸囧畾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()); + System.out.println("鍐欐枃浠跺け璐ワ細" + file.getAbsolutePath()); e.printStackTrace(); } finally { if (null != os) { @@ -261,17 +261,17 @@ /** * - * 查找文件需要的版本号的js 与css 行 - * @param line 行字符 - * @param version 默认版本 + * 鏌ユ壘鏂囦欢闇�瑕佺殑鐗堟湰鍙风殑js 涓巆ss 琛� + * @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> + //鍖归厤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){ @@ -279,7 +279,7 @@ break; } if(str.startsWith("/")){ - //如果已有版本号,按?分隔,取第一个 + //濡傛灉宸叉湁鐗堟湰鍙凤紝鎸�?鍒嗛殧锛屽彇绗竴涓� if(str.lastIndexOf("?version") != -1){ str = str.substring(0, str.lastIndexOf("?version")); } @@ -293,10 +293,10 @@ return modLine; } - //匹配html引入css文件 eg:<link href="/qxueyou/web/css/bootstrap.min.css" rel="stylesheet"> + //鍖归厤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){ @@ -304,7 +304,7 @@ break; } if(str.startsWith("/")){ - //如果已有版本号,按?分隔,取第一个 + //濡傛灉宸叉湁鐗堟湰鍙凤紝鎸�?鍒嗛殧锛屽彇绗竴涓� if(str.lastIndexOf("?version") != -1){ str = str.substring(0, str.lastIndexOf("?version")); } @@ -334,12 +334,12 @@ String version = df.format(date); Map<String,String> objMap = new HashMap<String,String>(); - //添加js文件 + //娣诲姞js鏂囦欢 getFileMD5Map(fileScanDirec,".js",objMap,version); - //添加CSS文件 + //娣诲姞CSS鏂囦欢 getFileMD5Map(fileScanDirec,".css",objMap,version); - //循环html文件,修改其中的css和js引入路径 + //寰幆html鏂囦欢锛屼慨鏀瑰叾涓殑css鍜宩s寮曞叆璺緞 getHtml(fileScanDirec,".ftl",objMap); System.out.println("successful"); -- Gitblit v1.8.0