[英]Cannot decode special character when using `IOUtils.toString(containerRequestContext.getEntityStream(),“UTF-8”); `
I use IOUtils.toString(containerRequestContext.getEntityStream(),"UTF-8");我使用 IOUtils.toString(containerRequestContext.getEntityStream(),"UTF-8"); to get the entity stream and use IOUtils to transfer it into string.
获取实体 stream 并使用 IOUtils 将其转换为字符串。 However, when meeting with some special character like "{", "[" and etc, IOUtils cannot decode them, only english and numbers and "&" can be decode.
但是,当遇到一些特殊字符如“{”、“[”等时,IOUtils 无法解码,只能解码英文和数字以及“&”。
The content of the entity decoded by IOUtls is as following IOUtls解码的实体内容如下
S=9&X=5&R=5&command=%5B%7B++++++++++%22zoneCode%22%3A+%22R98542%22%2C+++++++++++%22targetPosX%22%3A+4122.54%2C+++++++++++%22targetPosY%22%3A+9547.76%2C+++++++++++%22targetPosZ%22%3A+12548.69%2C+++++++++++%22power%22%3A+521456%2C+++++++++++%22duration%22%3A+6412+++++++%7D%2C+++++++%7B+++++++++++%22zoneCode%22%3A+%22R652485%22%2C+++++++++++%22targetPosX%22%3A+95482.36%2C+++++++++++%22targetPosY%22%3A+7845.85%2C+++++++++++%22targetPosZ%22%3A+9847.37%2C+++++++++++%22power%22%3A+741785%2C+++++++++++%22duration%22%3A+6482+++++++%7D%2C++++++%7B+++++++++++%22zoneCode%22%3A+%22R742545%22%2C+++++++++++%22targetPosX%22%3A+16982.93%2C+++++++++++%22targetPosY%22%3A+85623.17%2C+++++++++++%22targetPosZ%22%3A+4872.27%2C+++++++++++%22power%22%3A+34528%2C+++++++++++%22duration%22%3A+342+++++++%7D++%5D+
The primitive form data is as below: |field|value|原始表单数据如下: |field|value| |-|-|
|-|-| |S|9|
|S|9| |X|5|
|X|5| |R|5|
|R|5| |command|(text of a json array)|
|命令|(json 数组的文本)|
The value of "command" is shown as below: “命令”的值如下所示:
[
{
"zoneCode": "R98542",
"targetPosX": 4122.54,
"targetPosY": 9547.76,
"targetPosZ": 12548.69,
"power": 521456,
"duration": 6412
},
{
"zoneCode": "R652485",
"targetPosX": 95482.36,
"targetPosY": 7845.85,
"targetPosZ": 9847.37,
"power": 741785,
"duration": 6482
},
{
"zoneCode": "R742545",
"targetPosX": 16982.93,
"targetPosY": 85623.17,
"targetPosZ": 4872.27,
"power": 34528,
"duration": 342
}
]
You should also do a URL Decode.您还应该进行 URL 解码。 Import the
java.net.URLDecoder
package and use the URLDecoder.decode(string, encoding)
method.导入
java.net.URLDecoder
package 并使用URLDecoder.decode(string, encoding)
方法。
See an example below (byteToString variable is the text from your question):请参阅下面的示例(byteToString 变量是您问题中的文本):
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
String byteToString = "S=9&X=5&R=5&command=%5B%7B++++++++++%22zoneCode%22%3A+%22R98542%22%2C+++++++++++%22targetPosX%22%3A+4122.54%2C+++++++++++%22targetPosY%22%3A+9547.76%2C+++++++++++%22targetPosZ%22%3A+12548.69%2C+++++++++++%22power%22%3A+521456%2C+++++++++++%22duration%22%3A+6412+++++++%7D%2C+++++++%7B+++++++++++%22zoneCode%22%3A+%22R652485%22%2C+++++++++++%22targetPosX%22%3A+95482.36%2C+++++++++++%22targetPosY%22%3A+7845.85%2C+++++++++++%22targetPosZ%22%3A+9847.37%2C+++++++++++%22power%22%3A+741785%2C+++++++++++%22duration%22%3A+6482+++++++%7D%2C++++++%7B+++++++++++%22zoneCode%22%3A+%22R742545%22%2C+++++++++++%22targetPosX%22%3A+16982.93%2C+++++++++++%22targetPosY%22%3A+85623.17%2C+++++++++++%22targetPosZ%22%3A+4872.27%2C+++++++++++%22power%22%3A+34528%2C+++++++++++%22duration%22%3A+342+++++++%7D++%5D+";
String urlDecoded = URLDecoder.decode(byteToString , StandardCharsets.UTF_8.toString());
System.out.println(urlDecoded);
}
Result:结果:
S=9&X=5&R=5&command=[{ "zoneCode": "R98542", "targetPosX": 4122.54, "targetPosY": 9547.76, "targetPosZ": 12548.69, "power": 521456, "duration": 6412 }, { "zoneCode": "R652485", "targetPosX": 95482.36, "targetPosY": 7845.85, "targetPosZ": 9847.37, "power": 741785, "duration": 6482 }, { "zoneCode": "R742545", "targetPosX": 16982.93, "targetPosY": 85623.17, "targetPosZ": 4872.27, "power": 34528, "duration": 342 } ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.