// 复制文件
public void copyFile(File sourceFile, File targetFile, String storeId) {
try {
try {
// 读文件 ,
String content = readFile(sourceFile);
// 替换index_layout.vm中,css和js引用的@xxx.xxx.xxx@为真实的域名
StringBuffer html = new StringBuffer();
if (sourceFile.getName().contains("layout")) {
content = replaceSiteUrl(content);
// 在index_layout.vm中添加bizId的隐藏域
html.append(content);
html.append(
"<input type=\"hidden\" name=\"bizId\" id=\"bizId\" value=\"")
.append(storeId).append("\">");
} else {
// 替换index.vm中,css和js引用的@xxx.xxx.xxx@为真实的域名
content = replaceSiteUrl(content);
// 在index.vm中添加bizId的隐藏域
html = new StringBuffer();
html.append(
content.substring(0, content.indexOf("</body>")))
.append("<input type=\"hidden\" name=\"bizId\" id=\"bizId\" value=\"")
.append(storeId)
.append("\">")
.append(content.substring(content
.indexOf("</body>")));
}
writeFile(targetFile, html.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
}
}
//读文件
public static String readFile(File file) throws Exception {
StringBuffer content = new StringBuffer();
FileInputStream fis = null;
InputStreamReader reader = null;
try {
fis = new FileInputStream(file);
reader = new InputStreamReader(fis, CODE);
// 将输入流写入输出流
char[] buffer = new char[2048];
int n = 0;
while (-1 != (n = reader.read(buffer))) {
content.append(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
// log.error("读取文件【"+file.getPath()+"】出错", e);
throw new Exception("读取文件【" + file.getPath() + "】出错");
} finally {
try {
if (fis != null) {
fis.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return content.toString();
}
写文件
public static void writeFile(File file, String content) throws Exception {
BufferedWriter bufferedWriter = null;
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos, "UTF-8");
bufferedWriter = new BufferedWriter(osw);
bufferedWriter.write(content);
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
// log.error("写入文件【"+file.getPath()+"】出错", e);
throw new Exception("写入文件【" + file.getPath() + "】出错");
} finally {
try {
if (fos != null) {
fos.close();
}
if (osw != null) {
osw.close();
}
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 替换vm中css和js引用的@xxx.xxx.xxx@为真实的域名
private String replaceSiteUrl(String content) {
// \\s 空白符号、\\S 非空白符号
String regexp = "@(.*)@";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
String siteUrl = CustomizedPropertyPlaceholderConfigurer
.getInstance().getContextProperty(matcher.group(1));
content = content.replace(matcher.group(0), siteUrl);
}
return content;
}
// 复制文件
public void copyFile(File sourceFile, File targetFile, String storeId) {
try {
try {
// 读文件 ,
String content = readFile(sourceFile);
// 替换index_layout.vm中,css和js引用的@xxx.xxx.xxx@为真实的域名
StringBuffer html = new StringBuffer();
if (sourceFile.getName().contains("layout")) {
content = replaceSiteUrl(content);
// 在index_layout.vm中添加bizId的隐藏域
html.append(content);
html.append(
"<input type=\"hidden\" name=\"bizId\" id=\"bizId\" value=\"")
.append(storeId).append("\">");
} else {
// 替换index.vm中,css和js引用的@xxx.xxx.xxx@为真实的域名
content = replaceSiteUrl(content);
// 在index.vm中添加bizId的隐藏域
html = new StringBuffer();
html.append(
content.substring(0, content.indexOf("</body>")))
.append("<input type=\"hidden\" name=\"bizId\" id=\"bizId\" value=\"")
.append(storeId)
.append("\">")
.append(content.substring(content
.indexOf("</body>")));
}
writeFile(targetFile, html.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
}
}
//读文件
public static String readFile(File file) throws Exception {
StringBuffer content = new StringBuffer();
FileInputStream fis = null;
InputStreamReader reader = null;
try {
fis = new FileInputStream(file);
reader = new InputStreamReader(fis, CODE);
// 将输入流写入输出流
char[] buffer = new char[2048];
int n = 0;
while (-1 != (n = reader.read(buffer))) {
content.append(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
// log.error("读取文件【"+file.getPath()+"】出错", e);
throw new Exception("读取文件【" + file.getPath() + "】出错");
} finally {
try {
if (fis != null) {
fis.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return content.toString();
}
写文件
public static void writeFile(File file, String content) throws Exception {
BufferedWriter bufferedWriter = null;
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos, "UTF-8");
bufferedWriter = new BufferedWriter(osw);
bufferedWriter.write(content);
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
// log.error("写入文件【"+file.getPath()+"】出错", e);
throw new Exception("写入文件【" + file.getPath() + "】出错");
} finally {
try {
if (fos != null) {
fos.close();
}
if (osw != null) {
osw.close();
}
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 替换vm中css和js引用的@xxx.xxx.xxx@为真实的域名
private String replaceSiteUrl(String content) {
// \\s 空白符号、\\S 非空白符号
String regexp = "@(.*)@";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
String siteUrl = CustomizedPropertyPlaceholderConfigurer
.getInstance().getContextProperty(matcher.group(1));
content = content.replace(matcher.group(0), siteUrl);
}
return content;
}