gpt4 book ai didi

java - 使用 `IOUtils.toString(containerRequestContext.getEntityStream(),"UTF-8"); ` 时无法解码特殊字符

转载 作者:行者123 更新时间:2023-11-28 22:23:51 25 4
gpt4 key购买 nike

我使用 IOUtils.toString(containerRequestContext.getEntityStream(),"UTF-8");获取实体流并使用 IOUtils 将其转换为字符串。但是,当遇到一些特殊字符,如“{”、“[”等,IOUtils无法对其进行解码,只能解码英文和数字以及“&”。

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+

原始表单数据如下:|字段|值||-|-||S|9||X|5||R|5||命令|(json 数组的文本)|

“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
}
]

最佳答案

您还应该进行 URL 解码。导入 java.net.URLDecoder 包并使用URLDecoder.decode(string, encoding) 方法。

请参阅下面的示例(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);
}

结果:

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       }  ] 

关于java - 使用 `IOUtils.toString(containerRequestContext.getEntityStream(),"UTF-8"); ` 时无法解码特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58769149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com