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

Cannot decode special character when using `IOUtils.toString(containerRequestContext.getEntityStream(),"UTF-8"); `

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