生成静态文件,解决中文乱码

生成静态文件是提高访问速度的其中一种方法。
1.解决中文乱码的问题,需要考虑不同的字符集;
2.文件路径,需要考虑window和linux的兼容性,用File.separator
String xmlPath = request.getRealPath(File.separator).replace("\\",File.separator) + "filename.xml";


<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.io.*"%>
<%!
synchronized void writeHtml(String filePath, String info) {
	try {
		String path = filePath.substring(0, filePath.lastIndexOf(File.separator));
		File writePath = new File(path);		
		if (!writePath.exists()) {
		    writePath.mkdirs();
		}
		File writeFile = new File(filePath);
		boolean isExit = writeFile.exists();
		if (isExit != true) {
			writeFile.createNewFile();
		} else {
			writeFile.delete();
			writeFile.createNewFile();
		}
		OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(filePath),"GBK");
		BufferedWriter writer=new BufferedWriter(write);
		writer.write(info);
		writer.close();
	} catch (Exception ex) {
		System.out.println(ex.getMessage()+" File:"+filePath);
	} 
}
%>

你可能感兴趣的:(java,html,linux,xml)