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

问题描述 投票:0回答:1

我使用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       
}  
] 
java tomcat resteasy
1个回答
0
投票

您还应该进行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       }  ] 
© www.soinside.com 2019 - 2024. All rights reserved.