我是做Web开发的,项目使用的是SSH框架,项目要求把页面传递过来的参数封装成xml格式的字符流,然后传递到后台,在后台使用JDom解析字符流,然后把解析过后的数据填充到实体类中,最后把数据添加到数据库。现在我遇到的问题是中文乱码问题,在action中我把页面传递过来的字符流输出,控制台上打印出来的是中文,然后在service类中解析字符流,然后我再把解析过后的数据打印,结果控制台输出乱码以下是我解析字符流的代码: CustomerInfo customerInfo = new CustomerInfo();;
try {
//解析页面传递过来的xml格式的参数
//将字符串转换成输入流
InputStream inputStream = new ByteArrayInputStream(Parameters.getBytes());
//实例化一个SAXBuilder对象
SAXBuilder saxBuilder = new SAXBuilder();
//获得节点集合
List Nodelist = null;
InputStreamReader isr = new InputStreamReader(inputStream,"UTF-8");
Format format = Format.getPrettyFormat();
XMLOutputter xmlout = new XMLOutputter(format.setEncoding("UTF-8"));
//将输入流转换成Document对象
Document document = saxBuilder.build(isr);
//使用XPath指定节点位置
XPath NodexPath = XPath.newInstance(path);
Nodelist = NodexPath.selectNodes(document);
//遍历ROWDATA节点
for (int i = 0; i < Nodelist.size(); i++) {
//得到list集合里的Element对象
Element NodeElement = (Element) Nodelist.get(i);
//从Element对象中获取custInfoId属性值
Attribute CIDAttribute = NodeElement.getAttribute("custInfoId");
String custInfoId = CIDAttribute.getValue();
//填充到实体对象中
// System.out.println(custInfoId);
// if (!custInfoId.equals("undefined")||!custInfoId.equals("")) {
// customerInfo.setCustInfoId(Integer.parseInt(custInfoId));
// }
if (!custInfoId.equals("undefined")) {
if (!custInfoId.equals("")) {
customerInfo.setCustInfoId(Integer.parseInt(custInfoId));
}
}
System.out.println("custInfoId"+customerInfo.getCustInfoId());
//从Element对象中获取unitName属性值
Attribute UNAttribute = NodeElement.getAttribute("unitName");
String unitName = UNAttribute.getValue();
//填充到实体对象中
// if (!unitName.equals("undefined")&&!unitName.equals("")) {// }
if (!unitName.equals("undefined")) {
if (!unitName.equals("")) {
customerInfo.setUnitName(unitName);
}
}
// System.out.println("unitName"+unitName); //从Element对象中获取post属性值
Attribute POSTAttribute = NodeElement.getAttribute("post");
String post = POSTAttribute.getValue();
//填充到实体对象中
// if (!post.equals("undefined")||!post.equals("")) {// }
if (!post.equals("undefined")) {
if (!post.equals("")) {
customerInfo.setPost(Integer.parseInt(post));
}
}
// System.out.println("post"+post);
// System.out.println(customerInfo.getPost()); //从Element对象中获取province属性值
// Attribute PRAttribute = NodeElement.getAttribute("province");
// String province = PRAttribute.getValue();
//填充到实体对象
// customerInfo.setProvince(province); //从Element对象中获取city属性值
Attribute CITYAttribute = NodeElement.getAttribute("city");
String city = CITYAttribute.getValue();
// String newcity = new String(city.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(city);
if (!city.equals("undefined")) {
if (!city.equals("")) {
if(city.equals("北京")){
customerInfo.setProvince("北京");
}else if (city.equals("上海")) {
customerInfo.setProvince("上海");
}else if (city.equals("天津")) {
customerInfo.setProvince("天津");
}else if (city.equals("深圳")) {
customerInfo.setProvince("广东");
}else if (city.equals("东莞")) {
customerInfo.setProvince("广东");
}else if (city.equals("广州")) {
customerInfo.setProvince("广东");
}else if (city.equals("襄樊")) {
customerInfo.setProvince("湖北");
}else if (city.equals("香港")) {
customerInfo.setProvince("香港");
}else {
customerInfo.setProvince("province");
}
customerInfo.setCity(city);
}
}}} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return customerInfo; city输出之后在控制台显示的是乱码,我的项目全部是UTF-8编码,在项目中加了过滤器,也就是说乱码是在JDom解析字符流时产生的,请问这个问题该如何解决?
try {
//解析页面传递过来的xml格式的参数
//将字符串转换成输入流
InputStream inputStream = new ByteArrayInputStream(Parameters.getBytes());
//实例化一个SAXBuilder对象
SAXBuilder saxBuilder = new SAXBuilder();
//获得节点集合
List Nodelist = null;
InputStreamReader isr = new InputStreamReader(inputStream,"UTF-8");
Format format = Format.getPrettyFormat();
XMLOutputter xmlout = new XMLOutputter(format.setEncoding("UTF-8"));
//将输入流转换成Document对象
Document document = saxBuilder.build(isr);
//使用XPath指定节点位置
XPath NodexPath = XPath.newInstance(path);
Nodelist = NodexPath.selectNodes(document);
//遍历ROWDATA节点
for (int i = 0; i < Nodelist.size(); i++) {
//得到list集合里的Element对象
Element NodeElement = (Element) Nodelist.get(i);
//从Element对象中获取custInfoId属性值
Attribute CIDAttribute = NodeElement.getAttribute("custInfoId");
String custInfoId = CIDAttribute.getValue();
//填充到实体对象中
// System.out.println(custInfoId);
// if (!custInfoId.equals("undefined")||!custInfoId.equals("")) {
// customerInfo.setCustInfoId(Integer.parseInt(custInfoId));
// }
if (!custInfoId.equals("undefined")) {
if (!custInfoId.equals("")) {
customerInfo.setCustInfoId(Integer.parseInt(custInfoId));
}
}
System.out.println("custInfoId"+customerInfo.getCustInfoId());
//从Element对象中获取unitName属性值
Attribute UNAttribute = NodeElement.getAttribute("unitName");
String unitName = UNAttribute.getValue();
//填充到实体对象中
// if (!unitName.equals("undefined")&&!unitName.equals("")) {// }
if (!unitName.equals("undefined")) {
if (!unitName.equals("")) {
customerInfo.setUnitName(unitName);
}
}
// System.out.println("unitName"+unitName); //从Element对象中获取post属性值
Attribute POSTAttribute = NodeElement.getAttribute("post");
String post = POSTAttribute.getValue();
//填充到实体对象中
// if (!post.equals("undefined")||!post.equals("")) {// }
if (!post.equals("undefined")) {
if (!post.equals("")) {
customerInfo.setPost(Integer.parseInt(post));
}
}
// System.out.println("post"+post);
// System.out.println(customerInfo.getPost()); //从Element对象中获取province属性值
// Attribute PRAttribute = NodeElement.getAttribute("province");
// String province = PRAttribute.getValue();
//填充到实体对象
// customerInfo.setProvince(province); //从Element对象中获取city属性值
Attribute CITYAttribute = NodeElement.getAttribute("city");
String city = CITYAttribute.getValue();
// String newcity = new String(city.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(city);
if (!city.equals("undefined")) {
if (!city.equals("")) {
if(city.equals("北京")){
customerInfo.setProvince("北京");
}else if (city.equals("上海")) {
customerInfo.setProvince("上海");
}else if (city.equals("天津")) {
customerInfo.setProvince("天津");
}else if (city.equals("深圳")) {
customerInfo.setProvince("广东");
}else if (city.equals("东莞")) {
customerInfo.setProvince("广东");
}else if (city.equals("广州")) {
customerInfo.setProvince("广东");
}else if (city.equals("襄樊")) {
customerInfo.setProvince("湖北");
}else if (city.equals("香港")) {
customerInfo.setProvince("香港");
}else {
customerInfo.setProvince("province");
}
customerInfo.setCity(city);
}
}}} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return customerInfo; city输出之后在控制台显示的是乱码,我的项目全部是UTF-8编码,在项目中加了过滤器,也就是说乱码是在JDom解析字符流时产生的,请问这个问题该如何解决?
InputStreamReader isr = new InputStreamReader(inputStream,"UTF-8");
Format format = Format.getPrettyFormat();
XMLOutputter xmlout = new XMLOutputter(format.setEncoding("UTF-8"));
把UTF-8改成GBK就行了
InputStreamReader isr = new InputStreamReader(inputStream,"UTF-8");
Format format = Format.getPrettyFormat();
XMLOutputter xmlout = new XMLOutputter(format.setEncoding("UTF-8"));
把UTF-8改成GBK就行了