我的Google App Engine-servlet处理后请求。 它尝试获取XML字符串,但特殊字符会被更改。 像å/ä/ö这样的瑞典字符被转换成类似的东西:Ã¥
public String getXML(HttpServletRequest req){
String XML = "";
try {
BufferedReader b = req.getReader();
StringBuffer xmlBuffer = new StringBuffer();
while((XML = b.readLine()) != null) {
xmlBuffer.append(XML);
}
XML = xmlBuffer.toString();
} catch (IOException e) {
//maybe later
}
return XML;
}
后期数据始终是UTF-8的XML字符串,用于验证散列。
编辑问题:2013-12-05
答案 0 :(得分:0)
在这个构造中
new String(XML.getBytes(),"UTF-8")
你正在重新解释从platform's default charset到UTF-8
的字符串字节。
尝试在没有任何显式转换的情况下阅读请求,例如:
public String getXML(HttpServletRequest req){
try {
BufferedReader b = req.getReader();
StringBuffer xmlBuffer = new StringBuffer();
String XML;
while((XML = b.readLine()) != null) {
xmlBuffer.append(XML);
}
return xmlBuffer.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
或使用Apache IOUtils:
public String getXML(HttpServletRequest req) {
try {
return IOUtils.toString(req.getReader());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
getReader()
负责执行任何nessesary字符转换,如stated in the javadoc
读者根据身体上使用的字符编码翻译字符数据
答案 1 :(得分:0)
我设法找到(谷歌)答案..终于。
req.setCharacterEncoding("UTF-8");
public String getXML(HttpServletRequest req) throws IOException{
String input = null;
if ((input = (String) req.getAttribute("com.xp.input")) == null) {
StringBuilder buffer = new StringBuilder();
BufferedReader reader = req.getReader();
String line;
while((line = reader.readLine()) != null){
buffer.append(line);
}
input = buffer.toString();
req.setAttribute("com.xp.input", input);
}
if (input == null) {
return "";
}
String header = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
String body = input.substring(38, input.lastIndexOf('>')+1);
body = body.substring(body.indexOf('<'));
return header+"\n"+body+"\n";
}
这对我有用,我刚找到另一篇文章的代码,并添加了一些字符编码。