- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
public class SaasVersion {
//升级版本
private static final String v = "1.1";
//文件夹的绝对路径
private static final String[] pkgUrls = {
"D:/workspace/yz.saas/WebContent/views/assets/"
};
//要进行版本升级的资源文件
public static String[] pats = {".js",".css",".html"};
public static void main(String[] args) {
if (pkgUrls.length == 0) {
return;
}
for (int k = 0; k < pkgUrls.length; k++) {
isFileAndDirectory(pkgUrls[k]);
}
}
//判断是file还是目录;
//如果是目录,目录下所有的文件,迭代处理;
//如果是文件,则直接做版本替换处理,完事后删除原文件,把备份文件修改成原文件
private static void isFileAndDirectory(String url) {
File f = new File(url);
if (f.isDirectory()) {
File[] childUrls = f.listFiles();
if (null == childUrls || childUrls.length == 0) {
return;
} else {
for (File dirf : childUrls) {
isFileAndDirectory(dirf.getAbsolutePath());
}
}
} else {
changeVersion(url);
deleteAndRename(url);
}
}
//修改版本的业务代码
private static void changeVersion(String url) {
BufferedReader br = null;
PrintWriter pw = null;
try {
//处理原始文件
File f = new File(url);
if(!f.exists()) {
System.err.println("===============>文件不存在:url=" + url);
}
if (f.isDirectory()) {
System.err.println("===============>不能是文件夹:url=" + url);
}
//创建备份文件
String tmpUrl = url + ".bak";
File tmpFile = new File(tmpUrl);
pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmpFile), "UTF-8"));
//将要处理的文件,按行处理到缓存里面
br = new BufferedReader(new InputStreamReader(new FileInputStream(f),"UTF-8"));
String inputStr;
while ((inputStr = br.readLine()) != null) {
int patLength = 0;
String pat = "";
for (int j = 0; j < pats.length; j++) {
pat = pats[j];
patLength = pat.length();
if (inputStr.contains(pat)) {
int patIndex = inputStr.indexOf(pat);
String patNextChar = inputStr.substring(patIndex + patLength,patIndex + patLength + 1);
//如果*.js,*.css,.*.html 后面,不是引号或者问号; 说明不是带参或者无参,可能是方法; 比如*.html(),*.css("style","...")
if ("\"".equals(patNextChar)
|| "\'".equals(patNextChar)
|| "?".equals(patNextChar)) {
String str1 = inputStr.substring(0,patIndex + patLength);
String str11 = inputStr.substring(patIndex + patLength);
int leftIndex = 0;
int index1 = str11.indexOf("\'");
int index2 = str11.indexOf("\"");
if (index1 == -1 && index2 != -1) {
leftIndex = index2;
} else if (index1 != -1 && index2 == -1) {
leftIndex = index1;
} else {
leftIndex = index1 < index2 ? index1 : index2;
}
//开始替换
String str2 = str11.substring(0,leftIndex + 1);
String str3 = str11.substring(leftIndex + 1);
//str2 部分,是需要替换的路径部分
if (str2.contains("?") && str2.contains("v=")) {
//url后面已经有版本号
int vLen = "v=".length();
int midIndex = str2.indexOf("v=");
String lastStr = str2.substring(midIndex + vLen);
int lastLen = lastStr.length();
if (lastStr.contains("&")) {
str2 = str2.substring(0,midIndex + vLen) + v + lastStr.substring(lastStr.indexOf("&"));
} else {
str2 = str2.substring(0,midIndex + vLen) + v + lastStr.substring(lastLen - 1);
}
} else if (str2.contains("?") && !str2.contains("v=") ) {
//url后面没有版本号,但有其他内容
//eg: url="xxxx.html?cno=" + data.cno;
int wenHaoIndex = str2.indexOf("?");
String str21 = str2.substring(0,wenHaoIndex + 1);
String str22 = str2.substring(wenHaoIndex + 1);
str2 = str21 + "v=" + v + "&" + str22;
} else if(!str2.contains("?") && !str2.contains("v=") ) {
//url后面没有版本号,也没有?
str2 = "?v=" + v + str11.substring(leftIndex,leftIndex+1);
}
inputStr = str1 + str2 + str3;
System.out.println("===============>inputStr=" + inputStr);
System.out.println();
}
}
}
//换行
inputStr += "\r\n";
//写出文件,并且强制输出
pw.write(inputStr);
pw.flush();
}
} catch (UnsupportedEncodingException e) {
System.err.print(e);
} catch (FileNotFoundException e) {
System.err.print(e);
} catch (IOException e) {
System.err.print(e);
} finally {
try {
if (br != null) {
br.close();
}
if (pw != null) {
pw.close();
}
} catch (IOException e) {
System.err.print(e);
}
}
}
//删除原文件和重命名备份文件的方法
private static void deleteAndRename(String url) {
String bakUrl = url + ".bak";
//处理原始文件
File f = new File(url);
f.delete();
File bakf = new File(bakUrl);
bakf.renameTo(new File(url));
}
}