日期:2014-05-17 浏览次数:20794 次
public static void main(String[] args){
//这地方是用来测试的,测试结果是中文英文都不会乱码
test("E:/Software/Tomcat7.0/webapps/WebForJavaScript/Folder/stumess.xml");
}
public static void test(String filePath){
ConvertXmlToJson toJson=new ConvertXmlToJson();
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println(toJson.getXMLString(filePath)); //打印ConvertXmlToJson类的getXMLString()方法返回的字符串
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8"); //设置编码格式
response.setCharacterEncoding("UTF-8");
//response.setContentType("text/html;charset=utf-8");
//为什么这个地方打印出来的是乱码(中文乱码)?
test("E:/Software/Tomcat7.0/webapps/WebForJavaScript/Folder/stumess.xml");
public String getXMLString(String filePath) { //获取xml字符串
FileInputStream inputStream=null;
InputStreamReader inputStreamReader=null;
try {
inputStream = new FileInputStream(filePath); //文件字节流
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringBuffer buffer = new StringBuffer();
BufferedReader br = null;
String line;
try {
br = new BufferedReader(inputStreamReader);
while (true) {
line = br.readLine();
if (line == null){
break;
}
buffer.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return buffe